Run Script Not Working

I am making a run script for my game. I can’t seem to get the humanoid though. The error is saying " attempt to index nil with ‘WaitForChild’ "

Here is my script, anything is appreciated:


local RunningEvent = game.ReplicatedStorage:WaitForChild("Running")

local NotRunningEvent = game.ReplicatedStorage:WaitForChild("RunningEnded")

local IsRunning = false

local animation = game.StarterPlayer.StarterCharacterScripts.RunLocal.Running

game.Players.PlayerAdded:Connect(function(player)
	
	--Variables
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local Run = humanoid:LoadAnimation(animation)
	
	--Run
	RunningEvent.OnServerEvent:Connect(function(player)
		Run:Play()
		humanoid.WalkSpeed = 25
	end)
	
	--Stop running

	NotRunningEvent.OnServerEvent:Connect(function(player)
		Run:Stop()
		humanoid.WalkSpeed = 16
	end)
end)

This is happening because the player’s character doesn’t load with the player. You can use player.CharacterAdded for this.

But, you shouldn’t be creating a new event every single time a player joins your game! This will create multiple duplicate events that will create a huge memory leak over time. Instead, try something like this:

RunningEvent.OnServerEvent:Connect(function(player)
    if player.Character and player.Character:FindFirstChild("Humanoid") then
        Run:Play()
        player.Character.Humanoid.WalkSpeed = 25
    end
end)

Instead use

local character = player.Character or player.CharacterAdded:Wait()

Though this will only work for the first character, if you instead want it to work for every character in the future do:

player.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid")
	local Run = humanoid:LoadAnimation(animation)
	
	--Run
	RunningEvent.OnServerEvent:Connect(function(player)
		Run:Play()
		humanoid.WalkSpeed = 25
	end)
	
	--Stop running

	NotRunningEvent.OnServerEvent:Connect(function(player)
		Run:Stop()
		humanoid.WalkSpeed = 16
	end)
end)

So the result would be:

local RunningEvent = game.ReplicatedStorage:WaitForChild("Running")

local NotRunningEvent = game.ReplicatedStorage:WaitForChild("RunningEnded")

local IsRunning = false

local animation = game.StarterPlayer.StarterCharacterScripts.RunLocal.Running

game.Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local Run = humanoid:LoadAnimation(animation)
		
		--Run
		RunningEvent.OnServerEvent:Connect(function(player)
			Run:Play()
			humanoid.WalkSpeed = 25
		end)
		
		--Stop running
	
		NotRunningEvent.OnServerEvent:Connect(function(player)
			Run:Stop()
			humanoid.WalkSpeed = 16
		end)
	end)
end)
 Cannot load the AnimationClipProvider Service.

any way i can fix this?
So far I just did a variable for the service but I am not sure how to use it..

Well first of all I suggest to handle the animations + WalkSpeed client-sidedly mainly.
This would probably fix your struggles.
So just replace RunningEvent:FireServer() with

Run:Play()
humanoid.WalkSpeed = 25

and NotRunningEvent:FireServer() with

Run:Stop()
humanoid.WalkSpeed = 16

and put the follow variables at the start:

local animator = character:WaitForChild("Humanoid"):WaitForChild("Animator") 
local Run = animator:LoadAnimation(animation) -- Humanoid:LoadAnimation() is deprecated

but I suggest to put this script in StarterCharacterScripts instead of StarterPlayerScripts or whatever.

Will it show server sided too?

Animations handled client-sidedly will show server-sidedly as well, yes.
Though this is only for the client’s character.

I’m still confused… How do I set this up locally?

Well… Not locally but you get the idea

Parent the LocalScript to StarterCharacterScripts then

Firstly we need to define variables at the top:

local Animator = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator") -- Fix this path if needed, I am assuming that script.Parent is the character
local animation = -- define this
local Run = animator:LoadAnimation(animation) 

Wherever you send a message to the 1st event RunningEvent:FireServer(), you replace this with

Run:Play()
humanoid.WalkSpeed = 25

Then for the 2nd event NotRunningEvent:FireServer() you replace this with:

Run:Stop()
humanoid.WalkSpeed = 16

Then you are done.

So the variables play in the local or server script?

Animations played locally (on the client) will replicate to the server.