Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how do I make this repeat until Mouse.KeyUp()?

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)

1 answer

✓ Accepted answer

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)

2 comments

0

Oh okay I tried that but I did while sprinting == true do like it was a debounce

0

while sprinting do is equivalent to while sprinting == true do.

This discussion is preserved as it was. New answers, comments, and votes are closed.