• For loop not working for chat command args.
    8 replies, posted
if (string.sub(text, 1, 6) == "!addxp") then if table.HasValue(player.GetAll(), target) then for k, v in pairs( player.GetAll() ) do if v == target then local oldxpamount = target:GetPData("XP", 0) local newxpamount = oldxpamount + amountofxp target:SetPData("XP", newxpamount) timer.Simple( 0.25, function() sender:ChatPrint( "You gave " .. tostring( newxpamount ) .. " XP to " .. tostring( target ) ) end ) end end It seems to simply be returning the else message I put in that says an error occured, meaning it can't find the player. So, I tried using a for loop, but it definitely didn't work does anyone have a decent response to help? p.s. thanks for reading <3
Here's the same code with proper indenting. if (string.sub(text, 1, 6) == "!addxp") then     if table.HasValue(player.GetAll(), target) then         for k, v in pairs( player.GetAll() ) do             if v == target then                                  local oldxpamount = target:GetPData("XP", 0)                 local newxpamount = oldxpamount + amountofxp                                  target:SetPData("XP", newxpamount)                 timer.Simple( 0.25, function()                     sender:ChatPrint( "You gave " .. tostring( newxpamount ) .. " XP to " .. tostring( target ) )                 end )             end                      end                        It seems to me you don't have enough ends. Make your code more syntactically correct. If it still isn't working, you will have to post more code. This isn't complete enough to judge further than the missing ends.
The missing ends are further down in the code Full code: if (string.sub(text, 1, 6) == "!addxp") then if table.HasValue(player.GetAll(), target) then for k, v in pairs( player.GetAll() ) do if v == target then local oldxpamount = target:GetPData("XP", 0) local newxpamount = oldxpamount + amountofxp target:SetPData("XP", newxpamount) timer.Simple( 0.25, function() sender:ChatPrint( "You gave " .. tostring( newxpamount ) .. " XP to " .. tostring( target ) ) end ) end end else timer.Simple( 0.25, function() sender:ChatPrint( "Error occured. Check you entered a valid user, or a valid amount." ) end ) end end if text == ("!xp") then local currentxp = sender:GetPData("XP", 0) timer.Simple( 0.25, function() sender:ChatPrint( "You have " .. tostring( currentxp ) .. " xp." ) end ) end end
Ok, you actually included the else you were talking about this time (which is a good start), but I'm still not seeing where you set 'target' and 'newxpamount'. You could be failing to set 'target' properly, and nobody here would have any way of knowing. Post the actual full code. And fix your indenting. It vastly improves code readability.
Sadly, I can'f do much about that... The code block tool on these forums, enjoys fucking those up, and won't let me change them. And I would've posted the full code, but I thought it was a large amount to see and that i'd post first what I thought may be the most crucial part, and let anyone willing to help 'kindly' ask me for the rest of the code, like where certain things are defined and so on. function GM:PlayerSay( sender, text, teamChat ) local arguments = text:Split(" ")     local command = arguments[1] table.remove(arguments, 1) local target = arguments[1] local amountofxp = arguments[2] if (string.sub(text, 1, 6) == "!addxp") then if table.HasValue(player.GetAll(), target) then for k, v in pairs( player.GetAll() ) do if v == target then local oldxpamount = target:GetPData("XP", 0) local newxpamount = oldxpamount + amountofxp target:SetPData("XP", newxpamount) timer.Simple( 0.25, function() sender:ChatPrint( "You gave " .. tostring( newxpamount ) .. " XP to " .. tostring( target ) ) end ) end end else timer.Simple( 0.25, function() sender:ChatPrint( "Error occured. Check you entered a valid user, or a valid amount." ) end ) end end if text == ("!xp") then local currentxp = sender:GetPData("XP", 0) timer.Simple( 0.25, function() sender:ChatPrint( "You have " .. tostring( currentxp ) .. " xp." ) end ) end end
Ah, well there's the problem right there. You are setting 'target' to (presumably) a player name, which is a string. Player.GetAll() returns a table of player entities. A player's name will never be in that table, so your condition will always be false.
i spent 5 minutes on this but it should help you get started: hastebin
I prefer not to be spoonfed, but thanks for putting your time and effort into it... I can still use it as a reference, but I prefer to learn things rather than just copy paste them like a lot of people. I'm aware that it's a string, hence why I did the for loop but i'm thinking I may have put it in the wrong place or something?
You need to grab the player name off of the player entity, and compare against that. You may even want to use the pattern matching facilities in the string library to do partial name matches. if table.HasValue(player.GetAll(), target) then Remove that check completely, it's not helping you here. if v == target then Change this one to do what I said above.
Sorry, you need to Log In to post a reply to this thread.