• [urgent] Simple script lua, on why are the error ?
    8 replies, posted
i and friend BeZerk maked this code [CODE] function FreePoints(ply, text) if text == '!freepoints' then ply:SendLua([[gui.OpenURL("https://www.google.com")]]) timer.Simple( 5, function() ply:PS_GivePoints(50) ply:ChatPrint("You get 50 points") end ) chatcheck = text chatcheck.RefTime = CurTime() + 600 pan.Think = function( self, w, h ) if chatcheck.RefTime < CurTime() then return false and ply:ChatPrint("Please wait 10 minutes before using this command again") end end end end hook.Add( "PlayerSay", "Chat", FreePoints ) [/CODE] but us getting error on this [CODE] chatcheck = text chatcheck.RefTime = CurTime() + 600 pan.Think = function( self, w, h ) if chatcheck.RefTime < CurTime() then return false and ply:ChatPrint("Please wait 10 minutes before using this command again") end end [/CODE] in console i get this error [CODE] [ERROR] lua/teste01.lua:9: attempt to index global 'chatcheck' (a string value) 1. fn - lua/teste01.lua:9 2. Run - addons/ulib/lua/ulib/shared/hook.lua:110 3. func - addons/atlaschat/lua/atlaschat/init.lua:647 4. unknown - lua/includes/extensions/net.lua:32 [/CODE] if you know, can tell to me why i need make ? or why are the error. thanks i learn about strings and more some things but i still don't unknow
1.) Stop making new threads about the same broken piece of code every day. 2.) Format your code or else no one is going to want to read it [code] function FreePoints(ply, text) if text == '!freepoints' then ply:SendLua([[gui.OpenURL("https://www.google.com")]]) timer.Simple(5, function() ply:PS_GivePoints(50) ply:ChatPrint("You get 50 points") end) chatcheck = text chatcheck.RefTime = CurTime() + 600 pan.Think = function( self, w, h ) if chatcheck.RefTime < CurTime() then return false and ply:ChatPrint("Please wait 10 minutes before using this command again") end end end end hook.Add( "PlayerSay", "Chat", FreePoints ) [/code] 3.) All the issues: chatcheck is global, make it a local above the function. chatcheck is assigned as a string, but you try to use it like a table when you do: [code]chatcheck.RefTime = CurTime() + 600[/code] pan doesn't exist at all in this code - this think function is useless The useless think function has invalid syntax, returns are the last thing, you can't "return and" that just doesn't make sense: [code]return false and[/code] Here is working code, [b]actually read it and try to understand my comments[/b]: [code] local FREEPOINT_HISTORY = {} local function FreePoints(ply, text) if text == '!freepoints' then -- Check if they said it within the last 10 minutes if FREEPOINT_HISTORY[ply] and CurTime() - FREEPOINT_HISTORY[ply] < 600 then ply:ChatPrint("Please wait 10 minutes before using this command again") return '' end -- Store last command use FREEPOINT_HISTORY[ply] = CurTime() -- Open the URL ply:SendLua([[gui.OpenURL("https://www.google.com")]]) -- Wait 5 seconds and give 50 points timer.Simple(5, function() -- Make sure player is still valid if not IsValid(ply) then return end -- Give points ply:PS_GivePoints(50) ply:ChatPrint("You get 50 points") end) end end hook.Add( "PlayerSay", "Chat", FreePoints ) [/code]
[QUOTE=TehBigA;51780431]1.) Stop making new threads about the same broken piece of code every day. 2.) Format your code or else no one is going to want to read it [code] function FreePoints(ply, text) if text == '!freepoints' then ply:SendLua([[gui.OpenURL("https://www.google.com")]]) timer.Simple(5, function() ply:PS_GivePoints(50) ply:ChatPrint("You get 50 points") end) chatcheck = text chatcheck.RefTime = CurTime() + 600 pan.Think = function( self, w, h ) if chatcheck.RefTime < CurTime() then return false and ply:ChatPrint("Please wait 10 minutes before using this command again") end end end end hook.Add( "PlayerSay", "Chat", FreePoints ) [/code] 3.) All the issues: chatcheck is global, make it a local above the function. chatcheck is assigned as a string, but you try to use it like a table when you do: [code]chatcheck.RefTime = CurTime() + 600[/code] pan doesn't exist at all in this code - this think function is useless The useless think function has invalid syntax, returns are the last thing, you can't "return and" that just doesn't make sense: [code]return false and[/code] Here is working code, [b]actually read it and try to understand my comments[/b]: [code] local FREEPOINT_HISTORY = {} local function FreePoints(ply, text) if text == '!freepoints' then -- Check if they said it within the last 10 minutes if FREEPOINT_HISTORY[ply] and CurTime() - FREEPOINT_HISTORY[ply] < 600 then ply:ChatPrint("Please wait 10 minutes before using this command again") return '' end -- Store last command use FREEPOINT_HISTORY[ply] = CurTime() -- Open the URL ply:SendLua([[gui.OpenURL("https://www.google.com")]]) -- Wait 5 seconds and give 50 points timer.Simple(5, function() -- Make sure player is still valid if not IsValid(ply) then return end -- Give points ply:PS_GivePoints(50) ply:ChatPrint("You get 50 points") end) end end hook.Add( "PlayerSay", "Chat", FreePoints ) [/code][/QUOTE] Thank god, you ended this whole madness. I myself am a new coder and I understand all of what is going on in that code but I have a quick question, why would you store FREEPOINT_HISTORY[ply] ? Is it in-case you want to use it again in the function ?
It keeps track of when each player last said '!freepoints' and since it is outside the function it will remember it each time the function is run for reuse later.
[QUOTE=TehBigA;51780707]It keeps track of when each player last said '!freepoints' and since it is outside the function it will remember it each time the function is run for reuse later.[/QUOTE] Oh, thanks for the info :) never seen args have [] around them...I use ()...no idea what the difference is lol I assume it's just the same
[QUOTE=BeZerk;51780776]Oh, thanks for the info :) never seen args have [] around them...I use ()...no idea what the difference is lol I assume it's just the same[/QUOTE] () is used to call a variable, [] is used to index a variable
Not sure if you want it or if it's needed but I added checks to see if they are alive before showing the URL and giving the points....not sure if IsValid does death or connection checks on the player. [CODE]local FREEPOINT_HISTORY = {} local function FreePoints(ply, text) if text == '!freepoints' then -- Check if they said it within the last 10 minutes if FREEPOINT_HISTORY[ply] and CurTime() - FREEPOINT_HISTORY[ply] < 5 then ply:ChatPrint("Please wait 10 minutes before using this command again") return '' end -- keeps track of when each player last said '!freepoints' and since it is outside the function it will remember it each time the function is run for reuse later. FREEPOINT_HISTORY[ply] = CurTime() -- Open the URL if ply:Alive() == true then ply:SendLua([[gui.OpenURL("https://www.google.com")]]) elseif ply:Alive() == false then end -- Wait 5 seconds and give 50 points timer.Simple(0, function() -- Make sure player is still valid if not IsValid(ply) then return end -- Give points if ply:Alive() == true then ply:PS_GivePoints(50) ply:ChatPrint("You get 50 points") elseif ply:Alive() == false then ply:ChatPrint("You can't do this when dead") end end) end end hook.Add( "PlayerSay", "Chat", FreePoints )[/CODE] if anyone sees anything wrong be sure to correct me and I'll fix the checks
[QUOTE=BeZerk;51781146] [CODE] if ply:Alive() == true then ply:PS_GivePoints(50) ply:ChatPrint("You get 50 points") elseif ply:Alive() == false then ply:ChatPrint("You can't do this when dead") end [/CODE] [CODE] if ply:Alive() == true then ply:SendLua([[gui.OpenURL("https://www.google.com")]]) elseif ply:Alive() == false then end [/CODE] [/QUOTE] You don't need to use elseif in either of these if statements. You're already checking if they're alive so there's no need to check again. Just use else
Sorry, you need to Log In to post a reply to this thread.