You are having a problem where the character doesn't load in completely and your script still executes even though the character is not existent. A simple fix is to wait till the character loads in. You can do this with LocalPlayer.CharacterAdded:Wait() which will pause every single thing in the script till the character loads.
It's just a simple fix, you can do this:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(key)
if key == "0" then
local run = Instance.new('Animation')
run.AnimationId = 'rbxassetid://5013482632'
runAnim = Character.Humanoid:LoadAnimation(run)
runAnim:play()
Character.Humanoid.WalkSpeed = 20
end
end)
m.KeyUp:connect(function(key)
if key == "0" then
runAnim:stop()
local walk = Instance.new('Animation')
walk.AnimationId = 'rbxassetid://5013378526'
local walkAnim = Character.Humanoid:LoadAnimation(walk)
walkAnim:Play()
Character.Humanoid.WalkSpeed = 8
end
end)
Character.Humanoid.Running:Connect(function(speed)
local Humanoid = Character:WaitForChild("Humanoid")
if Character.Humanioid.WalkSpeed < 0.5 then
local idle = Instance.new('Animation')
idle.AnimationId = 'rbxassetid://5022482368'
local idleAnim = Character.Humanoid:LoadAnimaton(idle)
idleAnim:Play()
end
end)
Basically if the character is nil (non-existent) then we will wait till the character loads in.
But there is a flaw with this too. Whenever the character dies too, the script will reference the old character which is nil, (non existent because it died)
In order for that to work, we have to restate our variables as soon the character loads.
local Player = game.Players.LocalPlayer
local Character
Player.CharacterAdded:Connect(function(NewCharacter)
Character = NewCharacter
Character:WaitForChild("Humanoid")
end)
m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(key)
if key == "0" then
local run = Instance.new('Animation')
run.AnimationId = 'rbxassetid://5013482632'
runAnim = Character.Humanoid:LoadAnimation(run)
runAnim:play()
Character.Humanoid.WalkSpeed = 20
end
end)
m.KeyUp:connect(function(key)
if key == "0" then
runAnim:stop()
local walk = Instance.new('Animation')
walk.AnimationId = 'rbxassetid://5013378526'
local walkAnim = Character.Humanoid:LoadAnimation(walk)
walkAnim:Play()
Character.Humanoid.WalkSpeed = 8
end
end)
Character.Humanoid.Running:Connect(function(speed)
local Humanoid = Character:WaitForChild("Humanoid")
if Character.Humanioid.WalkSpeed < 0.5 then
local idle = Instance.new('Animation')
idle.AnimationId = 'rbxassetid://5022482368'
local idleAnim = Character.Humanoid:LoadAnimaton(idle)
idleAnim:Play()
end
end)
There! Now whenever you reset the character, you will get a brand new reference to the new character and won't run into nil character problems. I also removed repeat wait() until game.Players.LocalPlayer since the LocalPlayer always loads in first which means you'll never have loading problems for the player.
You should also make some variables to the humanoid and such, and use UserInputService. You can tell me if you want this added too.
Edit: here is the cleaned script just like you asked
local Player = game.Players.LocalPlayer
local Character
local Humanoid
Player.CharacterAdded:Connect(function(NewCharacter)
Character = NewCharacter
Humanoid = Character:WaitForChild("Humanoid")
end)
m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(key)
if key == "0" then
local run = Instance.new('Animation')
run.AnimationId = 'rbxassetid://5013482632'
runAnim = Humanoid:LoadAnimation(run)
runAnim:play()
Humanoid.WalkSpeed = 20
end
end)
m.KeyUp:connect(function(key)
if key == "0" then
runAnim:stop()
local walk = Instance.new('Animation')
walk.AnimationId = 'rbxassetid://5013378526'
local walkAnim = Humanoid:LoadAnimation(walk)
walkAnim:Play()
Humanoid.WalkSpeed = 8
end
end)
Character.Humanoid.Running:Connect(function(speed)
if Humanioid.WalkSpeed < 0.5 then
local idle = Instance.new('Animation')
idle.AnimationId = 'rbxassetid://5022482368'
local idleAnim = Humanoid:LoadAnimaton(idle)
idleAnim:Play()
end
end)
1 comment
Sorry for replying late ill edit my script for those variables