• Can I create a client function in the server side?
    6 replies, posted
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: [CODE] 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 [/CODE] 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 [I]ply.cansay[/I] the [I]cansay[/I] 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
[QUOTE=JasonMan34;51054702]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 startdelay( ply ) return ply.cansay end ) [/lua] When writing [I]ply.cansay[/I] the [I]cansay[/I] 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[/QUOTE] Thank you, I had thought about it but I did not. [editline]15th September 2016[/editline] [QUOTE=JasonMan34;51054702]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 startdelay( ply ) return ply.cansay end ) [/lua] When writing [I]ply.cansay[/I] the [I]cansay[/I] 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[/QUOTE] 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]
[QUOTE=Predda;51055391]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 end startdelay( ply ) return ply.cansay end ) [/lua][/QUOTE] 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.
[QUOTE=Predda;51055450]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.[/QUOTE] 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 [I]ret[/I] (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 [I]ret[/I] whose value will be equals to [I]ply.cansay[/I] at the time of sending the message
[QUOTE=JasonMan34;51055778]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 [I]ret[/I] (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 [I]ret[/I] whose value will be equals to [I]ply.cansay[/I] at the time of sending the message[/QUOTE] Ok, thank you!
Sorry, you need to Log In to post a reply to this thread.