• How to randomized one hook to be activated?
    20 replies, posted
I have many hooks, but how do we set it to be random select one hook to be activated out of many hooks?
I really dont see a reason why someone would randomized hooks being activated or not... are they all the same kind of hooks? if yes, it be smartner to make everything in one hook, and then a randomizer inside that one hook. Calling and removing hooks on a random chance/sequence seems..useless.. what for?
Moved to the correct section.
I see.. if so do i need to use something like table.Random ?
Could you give some example code of what exactly you're needing to randomise?
//FORBIDDEN ACTION 2 ( RUN ) hook.Add( "KeyPress", "NG_RUN", function( ply, key ) if ( key == IN_SPEED) then ply:Kill() end end ) //FORBIDDEN ACTION 3 ( no crouch ) hook.Add( "KeyPress", "NG_CROUCH", function( ply, key ) if ( key == IN_DUCK) then ply:Kill() end end ) Hi thanks for replying. I actually wanted to choose one of these 2 hooks in randomized way to activate for an ulx group
You can do this all in one hook, no need for multiple.
hook.Add( "KeyPress", "NG_RUN", function( ply, key ) if ( key == IN_JUMP) then ply:Kill() ply:ChatPrint("Forbidden Action: (Jump) has been triggered!") end if ( key == IN_DUCK) then ply:Kill() ply:ChatPrint("Forbidden Action: (Crouch) has been triggered!") end if ( key == IN_SPEED) then ply:Kill() ply:ChatPrint("Forbidden Action: (Run) has been triggered!") end end ) Yeah alright, like this? so how do i randomized it
local tKillKeys = {} -- Randomised every spawn -- Change to PlayerInitialSpawn to only randomise once hook.Add("PlayerSpawn", "RandomKillKey", function(pPlayer) -- Only add a kill key for users of a specific group if (pPlayer:IsUserGroup("some_group")) then tKillKeys[pPlayer] = math.random(0, 1) == 0 and IN_SPEED or IN_DUCK end end) hook.Add("KeyPress", "RandomKillKey", function(pPlayer, nKey) if (nKey == tKillKeys[pPlayer]) then pPlayer:Kill() end end) hook.Add("EntityRemoved", "RandomKillKey", function(pEntity) -- Clear out deleted player references if (pEntity:IsPlayer()) then tKillKeys[pEntity] = nil end end)
Very useful but it seems like it does nothing
Put some prints in to make sure the IsUserGroup check is passing.
hook.Add("PlayerInitialSpawn", "RandomKillKey", function(pPlayer)-- Only add a kill key for users of a specific group if (pPlayer:IsUserGroup("superadmin")) then         print ("check") tKillKeys[pPlayer] = math.random(0, 1) == 0 and IN_SPEED or IN_DUCK end end) Tried this but no output in console either
Try pPlayer:IsSuperAdmin() instead.
I wonder whats the problem, i didnt work i tried before codes and print is working fine.
Sounds like you aren't actually a super admin then.
hi there, i tried re write your codes and the print works only if the GM is different.
Then your gamemode is not letting you be superadmin. You can even print pPlayer:GetUserGroup() before the if-statement to see for yourself.
My bad, it didnt work if the hook is "PlayerSpawn" or "PlayerInitialSpawn"
Does the hook get called?
Yes its called. hmm...
Try printing out the usergroup before you check and see the result.
Sorry, you need to Log In to post a reply to this thread.