I made a sprinting script for my game and... there's energy and I want the script to stop the player from running if the energy either ='s 0 or the shift key is up my current system the energy still goes down even if the key triggering that part of the script isn't down
Here's my script :
local player = game.Players.LocalPlayer local Mouse = player:GetMouse() local empty = false local stamina = Instance.new("NumberValue", player); stamina.Value = 100 Mouse.KeyUp:connect(function(shiftUp) if shiftUp:byte() == 48 then player.Character.Humanoid.WalkSpeed = 16 end end) Mouse.KeyDown:connect(function(shiftDown) if shiftDown:byte() == 48 then player.Character.Humanoid.WalkSpeed = 45 while wait(0.1) do stamina.Value = stamina.Value -1 end end end)
What you need to do is add a conditional to the while loop, and set it to false when the key is released. Additionally, it's a good idea to put all the logic concerning modifying the player's speed in one place:
local player = game.Players.LocalPlayer local Mouse = player:GetMouse() local empty = false local stamina = Instance.new("NumberValue", player); stamina.Value = 100 local sprinting = false Mouse.KeyUp:connect(function(shiftUp) if shiftUp:byte() == 48 then sprinting = false end end) Mouse.KeyDown:connect(function(shiftDown) if shiftDown:byte() == 48 then player.Character.Humanoid.WalkSpeed = 45 sprinting = true while sprinting do stamina.Value = stamina.Value -1 wait(.1) end player.Character.Humanoid.WalkSpeed = 16 end end)