Can I create a client function in the server side?

Hello.
Excuse me for any grammatical errors.

I would like to create a delay I created an opposite function but the function starts the delay for all. I don’t know if you are understanding, an example:



local delay = 10
local cansay = true
hook.Add("PlayerSay", "playersay", function( ply )
timer.Simple(1, function()
startdelay()
end)
return cansay end)

local function startdelay()
cansay = false
timer.Simple(delay, function()
      cansay = true
end)
end



This works, but it doesn’t work fine, because if somebody says something, then the delay starts for all!

I believe what you’re looking for is player-specific variables.
So, with your code, it would look like this:

[lua]local delay = 10

local function startdelay( ply )
ply.cansay = false
timer.Simple( delay, function()
ply.cansay = true
end )
end

hook.Add( “PlayerSay”, “playersay”, function( ply )
if not ply.cansay then ply.cansay = true end
startdelay( ply )
return ply.cansay
end )
[/lua]

When writing ply.cansay the cansay variable is assigned to the player only, not the other players, so you can keep track of each player alone. This can be done to all entities.
Also - If you’re using local functions, make sure to define them before calling them in the code

Thank you, I had thought about it but I did not.

[editline]15th September 2016[/editline]

I’m sorry but didn’t you defined cansay?

Is correct to do something like this?:

[lua]local delay = 10
local cansay = true

local function startdelay( ply )
ply.cansay = false
timer.Simple( delay, function()
ply.cansay = true
end )
end

hook.Add( “PlayerSay”, “playersay”, function( ply )
if not ply.cansay then ply.cansay = true
startdelay( ply )
return ply.cansay
end )
[/lua]

Since in line 2, ply isn’t defined (Notice that ply only becomes defined once the function takes it as an argument in the PlayerSay hook). You also can’t localize a player-specific variable, simply because of how it is. The variable is assigned to the player, and you will now always access it through the player. Localizing it is impossible as you can’t localize an Entity

I edited the code look it now!

[editline]15th September 2016[/editline]

I’m reading with attention your code, and I am not understanding how it works.

I think that when you say something, only after 10 seconds you message will be sent, and it isn’t what I want to do.

I want that, when you send a message, after this message you won’t can sent another message before 10 seconds.

Ok so reading back the code I made some mistakes, this one would be the correct code: [lua]local delay = 10

local function startdelay( ply )
ply.cansay = false
timer.Simple( delay, function()
ply.cansay = true
end )
end

hook.Add( “PlayerSay”, “playersay”, function( ply )
local ret = false
if ply.cansay == nil then ply.cansay = true end
if ply.cansay then
ret = true
startdelay( ply )
end

return ret

end )[/lua]

However, that is not at all what is happening

When a player sends the very first message since spawning in - cansay will be nil, and thus set to true (So he can send messages).
Aside from the very first message, for every single message, if the player can send the message, it will set ret (The variable that will be returned - true/false) to true.
Then, it calls the function startdelay.
startdelay makes the player unable to send messages, then makes him able to send messeges 10 seconds later.
In the meantime, the hook returns ret whose value will be equals to ply.cansay at the time of sending the message

Ok, thank you!