Sprint doesn't work after reset

I made a sprint script which works completely fine in studio, even after I reset the character. Yet in a real Roblox server, the script no longer works after I reset.

This is a local script within StarterCharacterScripts

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local hrp = Character:WaitForChild("HumanoidRootPart")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("Animation"))
local Walk = Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running")

Walk.SoundId = "rbxassetid://174960816"
Walk.Volume = 4
Walk.PlaybackSpeed = 0.8

Animation.Priority = Enum.AnimationPriority.Movement

local running = false

local function ChangeFov(Fov, Time)
	TweenService:Create(workspace.Camera, TweenInfo.new(Time), {FieldOfView = Fov}):Play()
end


UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 40
		running = true
	end
end)

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 13
		running = false
	end
end)


RunService.Heartbeat:Connect(function()
	if running == true and Humanoid.MoveDirection.Magnitude > 0 then
		if not Animation.IsPlaying then
			Animation:Play()
			ChangeFov(105, 0.2)
			Walk.PlaybackSpeed = 1
		end
	else
		if Animation.IsPlaying then
			Animation:Stop()
			ChangeFov(100, 0.2)
			Walk.PlaybackSpeed = 0.8
		end
	end
end)

Any suggestions help, thank you!

When upon resetting the character. The game will have a few seconds or however time to load the character and so the script won’t or should get the character variable right away and will cause it to equals nil.

So instead we add a wait delay for the character to load like this example:

Also you shouldn’t use the humanoid for loading animations because it is Deprecated and should be using the Animator instance for loading animations

Another side note is to that not use too much WaitForChild functions because it will yeild the script in miliseconds.

On line Walk should just re use the hrp variable instead of having to WaitForChild again

Hope this helps!

Let me know if anything happens

Thank you!

Don’t mind this

It is still causing a problem. I tried implementing what you said

local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local hrp = Character:FindFirstChild("HumanoidRootPart")

local Animator = Humanoid:FindFirstChildOfClass("Animator")

local Animation = Animator:LoadAnimation(script:WaitForChild("Animation"))
local Walk = hrp:FindFirstChild("Running")

do you see any errors in the console at all

After a reset, the console displays “RunService:UnbindFromRenderStep removed different functions with same reference name utility-focus-state-inspect-MrHistoryMan 2 times.”

And that isn’t really an error

Oh, there are errors

i just tested it out myself it works for me when i reset.

local player = game:GetService("Players").LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local hrp = Character:FindFirstChild("HumanoidRootPart")

local Animator = Humanoid.Animator

--local Animation = Animator:LoadAnimation(script:WaitForChild("Animation"))
local Walk = hrp:FindFirstChild("Running")

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

Walk.SoundId = "rbxassetid://174960816"
Walk.Volume = 4
Walk.PlaybackSpeed = 0.8

--Animation.Priority = Enum.AnimationPriority.Movement

local running = false

local function ChangeFov(Fov, Time)
	TweenService:Create(workspace.Camera, TweenInfo.new(Time), {FieldOfView = Fov}):Play()
end


UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 40
		running = true
	end
end)

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 13
		running = false
	end
end)


RunService.Heartbeat:Connect(function()
	if running == true and Humanoid.MoveDirection.Magnitude > 0 then
		print("Running!")
		--if not Animation.IsPlaying then
		--	Animation:Play()
		--	ChangeFov(105, 0.2)
		--	Walk.PlaybackSpeed = 1
		--end
	else
		print("Not running")
		--if Animation.IsPlaying then
		--	Animation:Stop()
		--	ChangeFov(100, 0.2)
		--	Walk.PlaybackSpeed = 0.8
		--end
	end
end)

i just commented out some stuff and yeah it works for me, you can keep the “local Character = script.Parent”

The problem still persists for me. It works inside Studio but not on a regular Roblox server

that’s odd, i published the studio i put it in, and its working for me after reset. maybe its the animations? i commeneted the lines that had animations with it and its perfectly fine.

i had a little error where it said it failed to find humanoid, so i just set all the FindFirstChild to WaitForChild

Try using WaitForChild on the running sound variable

It throws an error on the running sound cause it is set to nil and can’t change an Id on nil

It just randomly started working. I’m going to test it a bit more to make sure that it continues to work.

Thanks for the help

I tested your script in game and the only error I really got was the sound id so all you had to do is set the sound variable to WaitForChild to give it time to load

Alright, I’ll try that. It randomly stopped working again :expressionless:

I get a different error where is says “attempt to index nil with ‘FindFirstChild’”

Would you mind showing the line?


I went into your script and did some changes and you should also disconnect events for the sake of not leaving memory leaks and so I simply organized it and on my way, tested it in studio and game too. It worked for me so hopefully this should work just fine

Let me know if there is any errors in the developer console or output.


local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local camera = game:GetService("Workspace").CurrentCamera

--[[Global Variables]]--

local player = game:GetService("Players").LocalPlayer

local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid.Animator

local running = false

local RunningSound = HumanoidRootPart:WaitForChild("Running")
RunningSound.SoundId = "rbxassetid://174960816"
RunningSound.Volume = 4
RunningSound.PlaybackSpeed = 0.8

local HeartbeatConnecton, InputBeganConnection, InputEndConnection, HumanoidConnection

--[[Functions]]--

local function ChangeFov(Fov: Number, Time: Number) TweenService:Create(camera, TweenInfo.new(Time), {FieldOfView = Fov}):Play() end

local function OnMove()
   if running and Humanoid.MoveDirection.Magnitude > 0 then
   	print("Running!")
   else
   	print("Not running")
   end
end

local function OnDeath()
   HumanoidConnection:Disconnect() InputBeganConnection:Disconnect() InputEndConnection:Disconnect() HeartbeatConnecton:Disconnect()
   HumanoidConnection = nil InputBeganConnection = nil InputEndConnection = nil HeartbeatConnecton = nil
end

--[[Events / Loops]]--

InputBeganConnection = UserInputService.InputBegan:Connect(function(input)
   if not (input.KeyCode == Enum.KeyCode.LeftShift) then return end
   Humanoid.WalkSpeed = 40 running = true
end)

InputEndConnection = UserInputService.InputEnded:Connect(function(input)
   if not (input.KeyCode == Enum.KeyCode.LeftShift) then return end
   Humanoid.WalkSpeed = 13 running = false
end)

HumanoidConnection = Humanoid.Died:Connect(OnDeath)
HeartbeatConnecton = RunService.Heartbeat:Connect(OnMove)

Blockquote