• Looping problem [GAMEMODE]
    8 replies, posted
I have a looping problem in my code, whenever I press Q or Z the code loops until I release the key (This code worked with GM:KeyPress before). What I'm trying to do is make the code loop only once even If the player holds Q or Z. [I][B]Before[/B][/I] you look at the codes, here is a small summery of what the whole thing does (Or suppose do to), When the player spawns, ply.Allahu would equal to 0 (Allahu is like a double safety feature). When the player hits Q, It would take the first safety off and If the player presses Q again, It would activate the bomb, so a sound would play before an explosion would set on the players current position. If the player hits Z when the "safety feature"(ply.Allahu) is equal to 0 (Or in English, Safety On) then It would play a sound and return. If the player hits Q, turning the safety off once, and then the player presses Z, ply.Allahu would be set to 0 (Safety would be on). [U][B]Code from cl_init.lua[/B][/U] [CODE] include("shared.lua") function safetyOff() if !input.IsKeyDown(KEY_Q) or !LocalPlayer().lock then if !LocalPlayer().lock then if input.IsKeyDown(KEY_Q) then LocalPlayer().lock=true net.Start("allahu_akbar") net.SendToServer() end else LocalPlayer().lock=false end end end function safetyOn() if !input.IsKeyDown(KEY_Z) or !LocalPlayer().lock then if !LocalPlayer().lock then if input.IsKeyDown(KEY_Z) then LocalPlayer().lock=true net.Start("allahu_notakbar") net.SendToServer() end else LocalPlayer().lock=false end end end hook.Add("Think","allahu_trigger",function() safetyOff() safetyOn() end) [/CODE] [U][B]Code from init.lua[/B][/U] [CODE] AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") resource.AddFile("sound/allahu.wav") resource.AddFile("sound/word.wav") util.AddNetworkString("allahu_akbar") util.AddNetworkString("allahu_notakbar") net.Receive("allahu_akbar",function(len,ply) if ply.Allahu==1 then local exp=ents.Create("env_explosion") exp:Spawn() exp:SetKeyValue("iMagnitude",200) sound.Play("allahu.wav",ply:GetPos(),70,100,1) ----> timer.Simple(1,function() [B]<--- [U]Lua error occurs here, Line 31[/U][/B] if ply:Alive() then exp:SetPos(ply:GetPos()) exp:Fire("Explode",0,0) else exp:Remove() end end) else sound.Play("weapons/slam/mine_mode.wav",ply:GetPos()) ply.Allahu=1 end end) net.Receive("allahu_notakbar",function(len,ply) if ply.Allahu==0 then sound.Play("word.wav",ply:GetPos()) else ply.Allahu=0 sound.Play("weapons/slam/buttonclick.wav",ply:GetPos()) end end) hook.Add("PlayerDeath","onPlayerDeath",function(ply) ply.Allahu=0 end) [/CODE] Here's a video that shows the problem better [B](First part is when I hold Q, second is holding Z)[/B], [video]https://youtu.be/VjkWKK6PTL8[/video] Thank you.
Try [CODE]input.IsKeyPressed[/CODE] instead of [CODE]input.IsKeyDown[/CODE]
[QUOTE=Gary D;50019719]Try [CODE]input.IsKeyPressed[/CODE] instead of [CODE]input.IsKeyDown[/CODE][/QUOTE] It didnt work. Besides that, I tried searching input.IsKeyPressed in the gmod wiki and I couldn't find anything about that command. Here is the code after I replaced the command, [CODE] function safetyOff() if !input.IsKeyPressed(KEY_Q) or !LocalPlayer().lock then if !LocalPlayer().lock then if input.IsKeyPressed(KEY_Q) then LocalPlayer().lock=true net.Start("allahu_akbar") net.SendToServer() end else LocalPlayer().lock=false end end end function safetyOn() if !input.IsKeyPressed(KEY_Z) or !LocalPlayer().lock then if !LocalPlayer().lock then if input.IsKeyPressed(KEY_Z) then LocalPlayer().lock=true net.Start("allahu_notakbar") net.SendToServer() end else LocalPlayer().lock=false end end end [/CODE] Heres the lua error, [url]http://imgur.com/gdI9QNL[/url]
[URL="http://wiki.garrysmod.com/page/input/WasKeyPressed"]It's WasKeyPressed[/URL]
[QUOTE=Mr.357;50022111][URL="http://wiki.garrysmod.com/page/input/WasKeyPressed"]It's WasKeyPressed[/URL][/QUOTE] I changed the IsKeyPressed to WasKeyPressed, doesn't work but no lua errors.
No solutions?
[QUOTE=MrNeon;50027487]No solutions?[/QUOTE] Add some chat.AddText or print commands to see if the code is being executed, just because you don't have syntax errors doesn't mean the code will work the way you want it to.
You're doing it inside a think hook, which runs constantly, and using a condition that returns true for every frame that key is held down - of course it's going to loop You'll want a cooldown, which can be done with CurTime(), that says 'okay when the player presses this key, record the time it was done. Next time they press the key, make sure enough time has past since the last press before executing the code again'
[QUOTE=NiandraLades;50029402]You're doing it inside a think hook, which runs constantly, and using a condition that returns true for every frame that key is held down - of course it's going to loop You'll want a cooldown, which can be done with CurTime(), that says 'okay when the player presses this key, record the time it was done. Next time they press the key, make sure enough time has past since the last press before executing the code again'[/QUOTE] Alright thank you!
Sorry, you need to Log In to post a reply to this thread.