Player can't set walkspeed and play animation after first death

Hello, I am working on a run script for my game.

local UserInputService = game:GetService("UserInputService")
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local animRun = Instance.new("Animation", script.Parent)
animRun.AnimationId = "rbxassetid://14680272756"
local loadRun = char:WaitForChild("Humanoid"):LoadAnimation(animRun)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		char.Humanoid.WalkSpeed = 35
		if loadRun then
			loadRun:Play()
		end
		print("started")
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		loadRun:Stop()
		print("stopped")
		char.Humanoid.WalkSpeed = 16
	end
end)

This script works when the player first spawns, but after dying it breaks. When I press LShift, I can see it still prints “started” however the walk speed stays the same and it doesnt play an animation.
After looking at other post, it may be because of the ragdoll script I’m using, but after analyzing the code I can’t find the cause of the issue.
Ragdoll script: https://create.roblox.com/marketplace/asset/7374049375/Ragdoll-Concept-Open-Source

After dying, the player.Character gets replaced with a new character and the char value will refer to the old character. When character spawns you can get the new character

local player = game.Players.LocalPlayer
player. CharacterAdded:Connect(function(newchar)
char = newchar
end) 
2 Likes

Alternatively, depending on preference, you can just always use player.Character instead of updating a char variable as player.Character always refers to the latest character.

Remember to reload your animations after death too.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.