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

Stamina/Sprint function not working properly? Any Ideas?

Asked by 8 years ago
Edited 8 years ago

Alright so I have this stamina/sprint (local) script and its not preforming like I expected... Instead of it actually letting you run, it doesn't.. 1. the while true do loops only runs once? 2. It wont let you run and stop running?

local InputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local IsSprinting = script.Parent.Variables.IsSprinting
while true do
    if(IsSprinting.Value == false) then
        if(script.Parent.Variables.Stamina.Value < 100) then
        wait(.25)
        script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value + 1
        script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
        return
        end
    end
end
while true do
    if(IsSprinting.Value == true) then
        if(script.Parent.Variables.Stamina.Value < 0) then
        wait(.05)
        script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value - 1
        script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
        return
        end
    end
end

InputService.InputBegan:connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        if script.Parent.Variables.Stamina.Value > 0 then
        IsSprinting.Value = true
        Humanoid.WalkSpeed = 25
        end
    end
end)

InputService.InputEnded:connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        IsSprinting.Value = false
        Humanoid.WalkSpeed = 16
    end
end)

1 answer

Log in to vote
0
Answered by
Versimn 20
8 years ago

You put a return in the while loops, ending them once reached. Just take them out and it should run fine

0
Oh, but what about the sprint functions? dotProgram 0 — 8y
0
By 'sprint functions' do you mean the functions connected to the event? If so, returns aren't needed. Versimn 20 — 8y
0
only use returns to 'Return' a value ex: func(val) return val+1 end print(func(0)) Versimn 20 — 8y
0
No, the function after the while true do dotProgram 0 — 8y
View all comments (3 more)
0
Those were the functions I was talking about. Versimn 20 — 8y
0
Oh how would I fix them? dotProgram 0 — 8y
0
'return' can also be used to return from a function's body prematurely, without returning values. Link150 1355 — 8y
Ad

Answer this question