Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Prioxis 673 Moderation Voter
9 years ago

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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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)

0
Oh okay I tried that but I did while sprinting == true do like it was a debounce Prioxis 673 — 9y
0
`while sprinting do` is equivalent to `while sprinting == true do`. adark 5487 — 9y
Ad

Answer this question