Sprinting Script didn't work

I got this script in the toolbox free model
script works as intended but when I reset my character it just stop

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")

local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = "rbxassetid://17406730342"
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Running = false

function Handler(BindName, InputState)
	if InputState == Enum.UserInputState.Begin and BindName == "RunBind" then
		Running = true
		Humanoid.WalkSpeed = 50
	elseif InputState == Enum.UserInputState.End and BindName == "RunBind" then
		Running = false
		if RAnimation.IsPlaying then
			RAnimation:Stop()
		end
		Humanoid.WalkSpeed = 16
	end
end

Humanoid.Running:connect(function(Speed)
	if Speed >= 10 and Running and not RAnimation.IsPlaying then
		RAnimation:Play()
		Humanoid.WalkSpeed = 30
	elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
		RAnimation:Stop()
		Humanoid.WalkSpeed = 16
	elseif Speed < 10 and RAnimation.IsPlaying then
		RAnimation:Stop()
		Humanoid.WalkSpeed = 16
	end
end)

Humanoid.Changed:connect(function()
	if Humanoid.Jump and RAnimation.IsPlaying then
		RAnimation:Stop()
	end
end)

game:GetService("ContextActionService"):BindAction("RunBind", Handler, true, Enum.KeyCode.LeftShift)
game.ReplicatedStorage.stopanimation.OnClientEvent:Connect(function()
	script:Destroy()
end)

maybe because you have this function that LITERALLY destroys the script

only when a specific event is triggered

  • when i checked the explorer it is still there but doesnt work

@err_nothing

put a print in there that way you know it isn’t firing at the wrong time

checked
It did not get deleted

try this:

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")

local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = "rbxassetid://17406730342"
local RAnimation = Humanoid:LoadAnimation(RunAnimation)

local Running = false
local RunBindName = "RunBind"

local function Handler(BindName, InputState)
    if InputState == Enum.UserInputState.Begin and BindName == RunBindName then
        Running = true
        Humanoid.WalkSpeed = 50
    elseif InputState == Enum.UserInputState.End and BindName == RunBindName then
        Running = false
        if RAnimation.IsPlaying then
            RAnimation:Stop()
        end
        Humanoid.WalkSpeed = 16
    end
end

local function OnRunningChanged(Speed)
    if Speed >= 10 and Running and not RAnimation.IsPlaying then
        RAnimation:Play()
        Humanoid.WalkSpeed = 30
    elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    elseif Speed < 10 and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    end
end

local function OnHumanoidChanged()
    if Humanoid.Jump and RAnimation.IsPlaying then
        RAnimation:Stop()
    end
end

local function OnRunBindAction()
    game:GetService("ContextActionService"):BindAction(RunBindName, Handler, true, Enum.KeyCode.LeftShift)
end

local function OnDestroy()
    game:GetService("ContextActionService"):UnbindAction(RunBindName)
    Humanoid.Running:Disconnect()
    Humanoid.Changed:Disconnect()
end

game:GetService("ContextActionService").ActionAdded:Connect(OnRunBindAction)
Humanoid.Running:Connect(OnRunningChanged)
Humanoid.Changed:Connect(OnHumanoidChanged)
game.ReplicatedStorage.stopanimation.OnClientEvent:Connect(OnDestroy)

show me the explorer, also i recommend not to use free models as most of them are broken code

It didn’t work when I joined the game, let alone when I reset my character

I have almost no experience in coding so I just used people’s code and implemented it

I changed some stuff see if this works now

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")

local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = "rbxassetid://17406730342"
local RAnimation = Humanoid:LoadAnimation(RunAnimation)

local Running = false
local RunBindName = "RunBind"
local RunBindAction

local function Handler(BindName, InputState)
    if InputState == Enum.UserInputState.Begin and BindName == RunBindName then
        Running = true
        Humanoid.WalkSpeed = 50
        RAnimation:Play()
    elseif InputState == Enum.UserInputState.End and BindName == RunBindName then
        Running = false
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    end
end

local function OnRunBindAction()
    if RunBindAction then
        RunBindAction:Disconnect()
    end
    RunBindAction = game:GetService("ContextActionService"):BindAction(RunBindName, Handler, false, Enum.KeyCode.LeftShift)
end

local function OnRunningChanged(Speed)
    if Speed >= 10 and Running and not RAnimation.IsPlaying then
        RAnimation:Play()
        Humanoid.WalkSpeed = 30
    elseif Speed < 10 and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    end
end

local function OnHumanoidChanged(Property)
    if Property == "Jump" and RAnimation.IsPlaying then
        RAnimation:Stop()
    end
end

local function OnDestroy()
    if RunBindAction then
        RunBindAction:Disconnect()
    end
    Humanoid.Running:Disconnect()
    Humanoid.Changed:Disconnect()
end

game:GetService("ContextActionService").ActionAdded:Connect(OnRunBindAction)
Humanoid.Running:Connect(OnRunningChanged)
Humanoid.Changed:Connect(OnHumanoidChanged)
game.ReplicatedStorage.stopanimation.OnClientEvent:Connect(OnDestroy)

did you actually check the explorer while playtesting to see if the script is still there? from where are you firing the stop.animation RemoteEvent?

the issue remains the same


but now it has an error

Ok maybe this script will work:

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")

local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = "rbxassetid://17406730342"
local RAnimation = Humanoid:LoadAnimation(RunAnimation)

local Running = false
local RunBindName = "RunBind"
local RunBindAction

local function Handler(BindName, InputState)
    if InputState == Enum.UserInputState.Begin and BindName == RunBindName then
        Running = true
        Humanoid.WalkSpeed = 50
        if not RAnimation.IsPlaying then
            RAnimation:Play()
        end
    elseif InputState == Enum.UserInputState.End and BindName == RunBindName then
        Running = false
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    end
end

local function OnRunBindAction()
    if RunBindAction then
        RunBindAction:Disconnect()
    end
    RunBindAction = game:GetService("ContextActionService"):BindAction(RunBindName, Handler, false, Enum.KeyCode.LeftShift)
end

local function OnRunningChanged(Speed)
    if Speed >= 10 and Running and not RAnimation.IsPlaying then
        RAnimation:Play()
        Humanoid.WalkSpeed = 30
    elseif Speed < 10 and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    end
end

local function OnHumanoidChanged(Property)
    if Property == "Jump" and RAnimation.IsPlaying then
        RAnimation:Stop()
    end
end

local function OnDestroy()
    if RunBindAction then
        RunBindAction:Disconnect()
    end
    Humanoid.Running:Disconnect()
    Humanoid.Changed:Disconnect()
end

game:GetService("ContextActionService").ActionAdded:Connect(OnRunBindAction)
Humanoid.Running:Connect(OnRunningChanged)
Humanoid.Changed:Connect(OnHumanoidChanged)
game.ReplicatedStorage.stopanimation.OnClientEvent:Connect(OnDestroy)

ActionAdded isn’t a valid event if I can remember.

Both doesn’t work at the start

did you try the solution I gave you? also send me the link to where you got this free model from

1 Like

I believe it is: BoundActionAdded | Roblox Wiki | Fandom

yeah you forgot to add Bound before actionadded

1 Like

change this to game:GetService(“ContextActionService”).BoundActionAdded:Connect(OnRunBindAction)