Running script not running after death

Hi, so I’m bad at scripting, but I threw together a running script which worked for the most part, but I just realised that once I die ingame, the running doesn’t work anymore. It runs with a string val which is either true or false in the starter character.

Local Script In starterPlayerScripts:

local UserInputService = game:GetService("UserInputService")
local LocalPlayer = game:GetService("Players").LocalPlayer;
local Character = LocalPlayer.Character

if not Character or not Character.Parent then
	Character = LocalPlayer.CharacterAdded:wait();
end

local Humanoid = Character:WaitForChild("Humanoid",3);

local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://16250185228'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)

function onInputBegan(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if Character.Sprinting.Value == "false" then
			Character.Sprinting.Value = "true"
			Humanoid.WalkSpeed = 25
			PlayAnim:Play()
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

function onInputEnded(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if Character.Sprinting.Value == "true" then
			Character.Sprinting.Value = "false"
			Humanoid.WalkSpeed = 18
			PlayAnim:Stop()
		end
	end
end

UserInputService.InputEnded:Connect(onInputEnded)

Video:

Please help me thanks for your time.

1 Like

You placed the script in StarterPlayerScripts. This script won’t reset after you die. Instead, put it in StarterCharacterScripts.

2 Likes

When you retrieve variables, and they have tasks such as WaitForChild, it will rerun the task before it returns anything. What’s happening is that your script for getting the Humanoid, calls back to Character which if they just spawned would be nil and break the entire script. Instead, you can put this instead.

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

So, if the player dies and the script is trying to look for the character it doesn’t return nil since the character doesn’t exist yet. Instead, it will either get the Character instantly (if it exists) or wait for it to be added.

Also remember that when the character dies, it is removed from the world and recreated. Your Character.Sprinting value will be deleted with it. Since you’re just using this as a local script it is completely unnecessary to create it on the Server. Let’s just create it on the client if it ever gets removed or deleted on Accident.

I also suggest using the Attributes introduced last February.

function onInputBegan(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then

		-- Anti-Error
		if not Character:GetAttribute("Sprinting") then
			Character:SetAttribute("Sprinting",false)
		end

		if Character:GetAttribute("Sprinting") == true then
			Character:SetAttribute("Sprinting",true)
			Humanoid.WalkSpeed = 25
			PlayAnim:Play()
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

function onInputEnded(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		-- Anti-Error
		if not Character:GetAttribute("Sprinting") then
			Character:SetAttribute("Sprinting",false)
		end

		if Character:GetAttribute("Sprinting") == true then
			Character:SetAttribute("Sprinting",false) 
			Humanoid.WalkSpeed = 18
			PlayAnim:Stop()
		end
	end
end
	
UserInputService.InputEnded:Connect(onInputEnded)

From what @kexy123 has said, I wouldn’t really suggest this because of how your script is setup. But, have a go at it!

Have fun.

Thank you so much! I didn’t know that it wouldn’t reset. Also thank you Lakota, but I fixed it.

1 Like

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