Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my variable not indexing after the 2nd time my event has been triggered?

I have been trying to make a Script where you can link yourself to a statue and when you'll die you'll respawn there but in the died event the CurrentPhoenixPos Value after the 2nd time it's triggered the print legit doesn't print anything.

repeat wait() until game.Players.LocalPlayer.Character

local phoenixEvent = game.ReplicatedStorage:WaitForChild("phoenixEvent")
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild("Humanoid")
currentPhoenixPos = script:WaitForChild("CurrentPhoenixPos")

phoenixEvent.OnClientEvent:Connect(function(posPart)
	currentPhoenixPos.Value = posPart.CFrame
end)

hum.Died:Connect(function()
	player.CharacterAdded:Wait()
	wait(0.1)
	repeat wait() until player.Character.HumanoidRootPart
	
	local char = player.Character
	print(currentPhoenixPos)
	char:SetPrimaryPartCFrame(currentPhoenixPos.Value)
end)

1 answer

✓ Accepted answer

The reason why is because "hum" will be nil after the character respawns. Put it all inside a CharacterAdded event handler and you'll be fine.

Eg.

--local variables
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

--Death event handler
local function AddDeathEvent(Char)
	Char.Humanoid.Died:Connect(function()
		--Your code here
	end)
end

--Rebind event handler when character dies
Player.CharacterAdded:Connect(function(Char)
	AddDeathEvent(Char)
end)

--First time
AddDeathEvent(Char)

This discussion is preserved as it was. New answers, comments, and votes are closed.