Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
5 years ago

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)
Ad

Answer this question