How would i make this script run again when the player respawns?

Heres the script.

local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
local rootpart,head = char:WaitForChild("HumanoidRootPart"),char:WaitForChild("Head")
game:GetService("RunService"):BindToRenderStep("CameraOffset",Enum.RenderPriority.Camera.Value-1,function()
	hum.CameraOffset = (rootpart.CFrame+Vector3.new(0,1.5,0)):pointToObjectSpace(head.CFrame.p)
end)

It’s supposed to lock the camera to the head.

1 Like

Lock the camera to the head? You mean like first person?

1 Like

You should try using Humanoid.Died. First execute the function with PlayerAdded and then after death.

local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			--function should run when humanoid respawns
		end)
	end)
end)
1 Like

its supposed to lock the camera to the head so that it can be used in third person

1 Like

Do you think you could put the two scripts together please?

1 Like

I think it’s a very bad practice, but I’m not too sure of a solution myself. Actually using Humanoid.Died isn’t necessary, as CharacterAdded will run every time character is added to the workspace, meaning respawning too.

local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local rootpart,head = char:WaitForChild("HumanoidRootPart"),char:WaitForChild("Head")
		game:GetService("RunService"):BindToRenderStep("CameraOffset",Enum.RenderPriority.Camera.Value-1,function()
			hum.CameraOffset = (rootpart.CFrame+Vector3.new(0,1.5,0)):pointToObjectSpace(head.CFrame.p)
		end)
	end)
end)
1 Like

If you want to be lazy, you can just put the camera script in StarterCharacterScripts or in a ScreenGui (or similar) that is set to reset on spawn. (Although, if you do use this method, make sure to disconnect any events you use to avoid possible memory leaks.) Otherwise, you’ll have to do some funky stuff:

local players = game:GetService('Players')
local localPlayer = players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local stuff = function()
	--do stuff
end
localPlayer.CharacterAdded:Connect(function(c)
	character = c
	stuff()
end)
stuff()

Or:

local runService = game:GetService('RunService')
local players = game:GetService('Players')
local localPlayer = players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
localPlayer.CharacterAdded:Connect(function(c)
	character = c
end)
runService.RenderStepped:Connect(function()
	if character then
		--do stuff
	end
end)