[urgent] Simple script lua, on why are the error ?

i and friend BeZerk maked this 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 )


but us getting error on this



		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


in console i get this error




[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


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

you just copied and pasted this code from the 5 other threads you’ve made in the past couple of days but ok

[lua] chatcheck = text
chatcheck.RefTime = CurTime() + 600[/lua]

you make chatcheck a string, and then immediately try to use it as a table, make chatcheck a table instead of a string

after you solve that, what is pan? the panel? you can’t access that serverside, idk why you’re trying to access it at all, you don’t need to.

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



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 )


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:


chatcheck.RefTime = CurTime() + 600

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:


return false and

Here is working code, actually read it and try to understand my comments:



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 )


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.

Oh, thanks for the info :slight_smile: never seen args have around them…I use ()…no idea what the difference is lol I assume it’s just the same

() 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.


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 )

if anyone sees anything wrong be sure to correct me and I’ll fix the checks

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