Everything breaks after the character resets!

I have this script where I update the billboard GUI’s labels, handle the eating mechanic, and manage the character’s size. Initially, everything works fine when the character joins. However, after resetting, the points label, the points value inside the player, and the name label no longer update. Also the size update, as well as the mechanics for fly eating and enemy player eating, stop functioning. Here is the “PlayerHandler” server script (Sorry if the script is messy).

local Players = game:GetService("Players")
local _workspace = game:GetService("Workspace")

local flyFolder = _workspace:WaitForChild("Flies")
local eatDist = 10

Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local Size = leaderstats:WaitForChild("Size")
	
	local char = plr.Character or plr.CharacterAdded:Wait()
	local Points = char:WaitForChild("PointsValue")
	local HRP = char:WaitForChild("HumanoidRootPart")
	local Gui = HRP:WaitForChild("DisplayGui")
	local Gui1 = HRP:WaitForChild("NameGui")
	local NameLabel = Gui1:WaitForChild("NameLabel")
	local PointsLabel = Gui:WaitForChild("PointsLabel")
		
	coroutine.resume(coroutine.create(function()
		while wait() do
			PointsLabel.Text = Size.Value
			Points.Value = Size.Value
			NameLabel.Text = plr.Name

			Size.Changed:Connect(function()
				char:ScaleTo(1 + Size.Value/100)
			end)
		end
	end))
	
	--rest of the script

To prevent this you should use plr.CharacterAdded(character)

local Players = game:GetService("Players")
local _workspace = game:GetService("Workspace")

local flyFolder = _workspace:WaitForChild("Flies")
local eatDist = 10

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
		local leaderstats = plr:WaitForChild("leaderstats")
		local Size = leaderstats:WaitForChild("Size")

		local char = plr.Character or plr.CharacterAdded:Wait()
		local Points = char:WaitForChild("PointsValue")
		local HRP = char:WaitForChild("HumanoidRootPart")
		local Gui = HRP:WaitForChild("DisplayGui")
		local Gui1 = HRP:WaitForChild("NameGui")
		local NameLabel = Gui1:WaitForChild("NameLabel")
		local PointsLabel = Gui:WaitForChild("PointsLabel")

		coroutine.resume(coroutine.create(function()
			while wait() do
				PointsLabel.Text = Size.Value
				Points.Value = Size.Value
				NameLabel.Text = plr.Name

				Size.Changed:Connect(function()
					char:ScaleTo(1 + Size.Value/100)
				end)
			end
		end))

		while wait() do

			--Handle FlyEat
			for i, flyModel in pairs(flyFolder:GetChildren()) do
				local flyBody = flyModel:FindFirstChild("Torso")

				if (HRP.Position - flyBody.Position).magnitude < eatDist then
					local flyPoints = flyModel:FindFirstChild("DisplayGui"):FindFirstChild("PointsAmount")

					if Size.Value >= flyPoints.Value then
						Size.Value = Size.Value + flyPoints.Value
						flyModel:Destroy()
					end
				end
			end

			--Handle player kill
			for i, enemyPlayer in pairs(Players:GetPlayers()) do
				if enemyPlayer.Name ~= plr.Name then
					local EnemyChar = enemyPlayer.Character or enemyPlayer.CharacterAdded:Wait()
					local EnemyHRP = EnemyChar:FindFirstChild("HumanoidRootPart")

					if (HRP.Position - EnemyHRP.Position).magnitude < eatDist then
						local enemyPlayerPoints = char:FindFirstChild("PointsValue")

						if Size.Value >= enemyPlayerPoints.Value then
							Size.Value = Size.Value + enemyPlayerPoints.Value
							EnemyChar:FindFirstChild("Humanoid").Health = 0
						end
					end
				end
			end
		end
	end)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.