The local script functions perfectly fine when initially joining/spawning.
However, after resetting, the script throws errors, that the Instance is not a valid Member of X, I checked, and the Instance IS actually parented correctly. So I have no Idea why this error is thrown.
Code Snippet Here, Only relevant code
local HUD =Player.PlayerGui:WaitForChild("HUD")
local PlayerCard =HUD:WaitForChild("PlayerCard")
local function updateInfo(Target:Model|Humanoid)
-- Set initial UI values
PlayerCard.PlayerName.Text =Target.Parent.Name
PlayerCard.ID.Text =(TargetPlayer and TargetPlayer.UserId) or "N/A"
end
Mouse.Move:Connect(function()
if Mouse.Target and Mouse.Target.Parent and Mouse.Target.Parent:FindFirstChildWhichIsA("Humanoid") then
updateInfo(Mouse.Target)
end)
Your LocalScript runs once when the player first joins, which is when the first HUD is created. However since the HUD has ResetOnSpawn enabled, when the player dies and respawns, the old HUD is destroyed along with the old PlayerCard, and the script doesn’t know about the new HUD.
You can solve this either by moving the script into the HUD, or listening for when the player respawns and chaning the HUD and PlayerCard variables to reference the new HUD.
local HUD =Player.PlayerGui:WaitForChild("HUD")
local PlayerCard =HUD:WaitForChild("PlayerCard")
local KillContainer =HUD:WaitForChild("KillContainer")
Player.CharacterAdded:Connect(function(Character)
--Update GUI VAR
HUD =Player.PlayerGui:WaitForChild("HUD")
PlayerCard =HUD:WaitForChild("PlayerCard")
KillContainer =HUD:WaitForChild("KillContainer")
end)