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

KeyDown sprint function not working?

Asked by 7 years ago

I am making a stamina/sprint script and I made a function that lets you go faster but its not working?

player = script.Parent.Parent.Parent
while true do
    wait(.25)
    if(script.Parent.Variables.Stamina.Value < 100) then
        script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value + 1
        script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
    end
end

function onKeyDown(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        if (script.Parent.Variables.Stamina.Value > 0) then
            local humanoid = player.Character:FindFirstChild("Humanoid")
            humanoid.WalkSpeed = 25
        end
    end
end
game:GetService("UserInputService").InputBegan:connect(onKeyDown)

And how would I make it to were when the key goes up, it stops the Sprint

0
Sorry I don't have time to answer this, but I can give you something that will help. Here's a video I made on it: https://www.youtube.com/watch?v=9bggLpCFMOg, and the module: https://www.roblox.com/Custom-events-library-Create-or-modify-events-item?id=398814042 ScriptGuider 5640 — 7y
0
Please look at my answer below I provided, It adds to the stamina, but It wont increase my speed dotProgram 0 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
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 IsSprinting.Value == false do
    if(script.Parent.Variables.Stamina.Value < 100) then
    IsSprinting.Value = false
    wait(.25)
    script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value + 1
    script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
    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
            while IsSprinting.Value == true do
                wait(.05)
                script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value - 1
            end
        end
    end
end)

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

Answer this question