#Sprint mechanic not working when player presses another key which is a keybind for a UI

1 messages · Page 1 of 1 (latest)

fierce mica
#
local RNS = game:GetService("RunService")


local StaminaDrain = 0.1
local RechargeTime = 1

local char = script.Parent
local hum = char:WaitForChild("Humanoid")

local lastSprint = math.huge

local isSprinting = false

local function sprint(actionName, inputState, inputType)
    print("Sprinting")
    if inputState == Enum.UserInputState.Begin then
        isSprinting = true
    elseif inputState == Enum.UserInputState.End then
        isSprinting = false
    end

    if isSprinting then
        hum.WalkSpeed = hum:GetAttribute("SprintSpeed")
    else
        hum.WalkSpeed = hum:GetAttribute("BaseWalkSpeed")
    end
end


CAS:BindAction("Sprint", sprint, true, Enum.KeyCode.LeftShift)

RNS.RenderStepped:Connect(function(delta)
    local Stamina = hum:GetAttribute("Stamina")
    if Stamina < StaminaDrain then
        isSprinting = false
        hum.WalkSpeed = hum:GetAttribute("BaseWalkSpeed")
    end

    if isSprinting and Stamina > 0 then
        hum:SetAttribute("Stamina", Stamina - StaminaDrain)
        lastSprint = tick()
    else
        if tick() - lastSprint >= RechargeTime and Stamina < hum:GetAttribute("MaxStamina") then
            hum:SetAttribute("Stamina", Stamina + .3)
        end
    end
end)```
#

Whenever a player then clicks for example "F" which opens up a GUI in the game, the player can no longer sprint and the sprint function does not fire

#

When the player begins sprinting it prints "Sprinting" once and then again when the player stops sprinting

#

This is the scrpt to open the UI

#

which breaks it, I think

#
    if typing then return end
    if input.KeyCode == Enum.KeyCode.F then
        UIclick:Play()
        if debounce == 0 then 
            debounce = 1 
            CloneChar()
            for i = 1,10 do 
                wait(0.02)
                MenuFrame.Position = MenuFrame.Position + UDim2.new(0.012,0, 0,0) 
            end
            debounce = 2 
            
        elseif debounce == 2 then 
            debounce = 1 
            DestroyChar()
            ChoiceFrames.Visible = false
            for i = 1,10 do
                wait(0.02)
                MenuFrame.Position = MenuFrame.Position - UDim2.new(0.012,0, 0,0)
                debounce = 0 
            end
        end
    end
end)```
#

Thanks