Script wont work after respawn

Im looking for a way to have my script work even after the player respawns. The script works just fine until the player dies and responds.

Its a control to run script. Its placed in the starter pack. The animation is by @Shonny_Yeet , i made the code.

Heres the script

Code
--Variables
local player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")
local animation = script.Animation
local track = humanoid:LoadAnimation(animation)

local bool = false
local camera = game.Workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
-----------------------------------------------------------------
local Info = TweenInfo.new(5)
local tween = TweenService:Create(camera,Info, {["FieldOfView"] = 95}) --Smoothly makes teh FieldOfView bigger, to simulate running.

local tween2 = TweenService:Create(camera,Info, {["FieldOfView"] = 70}) --Smoothly make the FieldOfView smaller, to simulate coming to a stop.

-----------------------------------------------------------------
UIS.InputBegan:Connect(function (input, chat)
	if chat then return end --If you press left control while the chat is open,script will return end
	
	if input.KeyCode == Enum.KeyCode.LeftControl then --If you press left control,
		bool = true
		humanoid.WalkSpeed = 32
		track:Play()
		tween:Play()
		while bool == true do --Detects if the player is holding the button down
			wait(1)
			print(player.Name, "is running") --Will print if the player is holding down left shift
			
		end
	end
end)

UIS.InputEnded:Connect(function (input, chat)
	if chat then return end --If you press left control while the chat is open,script will return end

	if input.KeyCode == Enum.KeyCode.LeftControl then --If you press left control
		
		camera.CameraType = Enum.CameraType.Custom
		track:Stop()
		print(player.Name, "is no longer running!")
		humanoid.WalkSpeed = 16
		tween2:Play()
	end
end)

Help is appreciated, thank you for your time :grinning:

EDIT: I got this error in the output: LoadAnimation requires the Humanoid object (Pyromxnia.Humanoid) to be a descendant of the game object

I think that this issue is that after the character is reset the humanoid variable is still set to the old humanoid. This might be because you are keeping the script in StarterPack instead of StarterPlayerScripts. Also, you should use: local track = humanoid.Animator:LoadAnimation(animation) instead of
local track = humanoid:LoadAnimation(animation). I’m pretty sure humanoid:LoadAnimation() is being deprecated.

1 Like

Place the script inside the StarterCharacterScripts folder if you want it to execute for each player each time his/her character model is reloaded/spawned.

1 Like

Doing humanoid:LoadAnimation() initially creates the Animator object which can then be used to load future animations.

The animator object should be created when the character is spawned by default.

Oh ok, I wasn’t aware of that, I thought it was loading by default because Roblox loads the default animations as soon as you spawn.

I assume it will likely be loaded automatically when Humanoid is eventually deprecated in regards to loading animations.

Yeah, I was also thinking that; it wouldn’t make sense if you had to use a deprecated function to use the new one.

Try this one


--Variables
local player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")
local animation = script.Animation
local track = humanoid:LoadAnimation(animation)

repeat task.wait() until Humanoid 

local bool = false
local camera = game.Workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
-----------------------------------------------------------------
local Info = TweenInfo.new(5)
local tween = TweenService:Create(camera,Info, {["FieldOfView"] = 95}) --Smoothly makes teh FieldOfView bigger, to simulate running.

local tween2 = TweenService:Create(camera,Info, {["FieldOfView"] = 70}) --Smoothly make the FieldOfView smaller, to simulate coming to a stop.

-----------------------------------------------------------------
UIS.InputBegan:Connect(function (input, chat)
	if chat then return end --If you press left control while the chat is open,script will return end
	
	if input.KeyCode == Enum.KeyCode.LeftControl then --If you press left control,
		bool = true
		humanoid.WalkSpeed = 32
		track:Play()
		tween:Play()
		while bool == true do --Detects if the player is holding the button down
			wait(1)
			print(player.Name, "is running") --Will print if the player is holding down left shift
			
		end
	end
end)

UIS.InputEnded:Connect(function (input, chat)
	if chat then return end --If you press left control while the chat is open,script will return end

	if input.KeyCode == Enum.KeyCode.LeftControl then --If you press left control
		
		camera.CameraType = Enum.CameraType.Custom
		track:Stop()
		print(player.Name, "is no longer running!")
		humanoid.WalkSpeed = 16
		tween2:Play()
	end
end)

every time you do

do instead
game.Players.LocalPlayer.Character
you are still referencing the old character

Theres a small point when the character is respawning that it is parented to nil and not a descendant to the game object.
You’ll have to wait for it to be under the game object to use LoadAnimation.
Putting some kind of wait at the start can solve this.
Ex:

wait(.1) -- pretty sure .1 is more than enough
-- or
repeat
    wait()
until humanoid:IsDescendantOf(game) -- Or you can use humanoid.Parent == workspace

Also you should use a Animator object to load the animation as others have said, They are created along with the character and are under its humanoid.

1 Like