Local Script gets parented to nil, executes functions, after player dies

Description of my problem:
I have a local script in “PlayerStarterScripts”, in this script I have a function, e.g: change text of a TextLabel.

Now, when the player dies, it will ofc receive new GUI, and local scripts, however, I get errors, from the previous local script, which keeps on stacking.

Image of Errors

The script is parented to nil, because i tried to print script.Parent.Name > Output: nil

Now the problem is, I don’t know how to fix it, and more importantly, I don’t know if this bug is causes by Warp, a networking library. THis is because the Function which handles the text change, is initiallized with help of Warp.

My Local Script:

Local Script
ReplicatePlayerDataRE:Connect(function(PlayerData:table)
	if PlayerData["Faction"]["Name"] then
		local FactionData = GetFactionDataRF:Invoke(10, PlayerData["Faction"]["Name"])
		if FactionData then
			UpdateGUIElements(FactionData)
			
		elseif localFacionData["Name"] then -- Failsave
			UpdateGUIElements(localFacionData)
		end
	else
		if script.Parent then
			CanvasTop.Banner.FactionName.Text = "Faction: None"
		end
	end
end)
The function for changing text
local function UpdateGUIElements(Data:table)
	local CoOwnerID
	for id, role in pairs(Data.Members) do
		if role == "CoOwner" then
			CoOwnerID = id
			break
		end
	end
	-- CanvasTop
	FactionGUIElements.FactionName.Text	="["..Data.Tag.."] "..Data.Name
	FactionGUIElements.Description.Text	=Data.Desc
	FactionGUIElements.Icon.Image		="rbxthumb://type=Asset&id="..Data.Icon.."&w=420&h=420"
	FactionGUIElements.Banner.Image		="rbxthumb://type=Asset&id="..Data.Banner.."&w=420&h=420"
	
	-- CanvasMiddle
	task.wait(1)
	for _,v in ipairs(FactionGUIElements.Information.FactionName:GetChildren()) do
		print(v.Name)
	end
	
	--Works
	FactionUI				=PlayerGui.FactionUI
	FactionUI.CanvasGroup.Middle.Information.FactionName.TextLabel.Text		="Faction: "..Data.Name
	
	--Error
	--FactionUI.CanvasGroup.Middle.Information.FactionName.TextLabel.Text		="Faction: "..Data.Name
	
	--Error
	--game.Players.LocalPlayer.PlayerGui.FactionUI.CanvasGroup.Middle.Information.FactionName.TextLabel.Text			="Faction: "..Data.Name
	
	
	--FactionGUIElements.Information.FactionName.TextLabel.Text	="Faction: "..Data.Name
	FactionGUIElements.Information.Tag.TextLabel.Text			="Tag: ["..Data.Tag.."]"
	FactionGUIElements.Information.Description.TextLabel.Text	="Description: "..Data.Desc
	FactionGUIElements.Information.Owner.TextLabel.Text			="Owner: "..GetUsernameByIDRF:Invoke(10, Data.Owner)
	FactionGUIElements.Information.CoOwner.TextLabel.Text		="Co-Owner: "..(GetUsernameByIDRF:Invoke(10, CoOwnerID) or "None")
	FactionGUIElements.Information.MemberCount.TextLabel.Text	="MemberCount: "..MainModule.dictionaryCount(Data.Members).." / "..Data.Restrictions.MaxMembers
	FactionGUIElements.Information.UniqueID.TextLabel.Text		="UniqueID: "..Data.Restrictions.UniqueID
end

As a rule of thumb for Roblox development, never have scripts that are created or destroyed. It will lead to so many engine-related issues like these and ruin your codebase.

There is no reason this script should be in PlayerStarterScripts to begin with. Just make it a normal script with a client RunContext and place it in ReplicatedStorage or somewhere else that replicates. GUIs can also be set to not reset on spawn.


By the way, table is not a proper type annotation. You would want to have a record type to represent PlayerData.

type PlayerData = {
	Faction: {
		Name: string?
	}
	-- etc.
}

removed the table annotion

Make the FactionUI not reset on respawn

make the lcoal script into a normal script, client runcontext and placed in replicated storage.

Now I’m getting an error from Warp for sure