Why does the UI Stay on the Screen when the Player Dies?

Hello, I am trying to make a local script that displays a certain number of points when a player touches a coin.So if a player touches a coin, they would get +2 displayed on the screen, and then it would disappear after 1.2 seconds. The problem with this is that if the player dies while the 1.2 seconds is not elapsed yet, the UI Stays on the screen forever. There are no errors. Here is my localscript.

local collectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local Sounds = workspace:WaitForChild("Sounds")
local coinSound = Sounds:WaitForChild("CoinSound")
local player = Players.LocalPlayer
local gui = script.Parent.Parent:WaitForChild("Towers")
local textLabelPoints = gui:WaitForChild("Points")

local remoteEvent = game.ReplicatedStorage:WaitForChild("AddPointsCandy")
local GummyBearCoin = game.ReplicatedStorage:WaitForChild("GummyBearCoinValue").Value



local humanoid = player.Character:WaitForChild("Humanoid")

local debounce = false

humanoid.Touched:Connect(function(Hit)
	if collectionService:HasTag(Hit, "GummyBearCoin") then
		
		
		if not debounce then
			local Num = math.random(1, 4)
			debounce = true
			remoteEvent:FireServer(Hit)
			
			local textLabel = textLabelPoints:Clone()
			textLabel.Parent = gui
			textLabel.Name = "PointsClone"
			
			if Num == 1 then
				textLabel.Position = UDim2.new(0.639, 0, 0.761, 0)

			elseif Num == 2 then
				textLabel.Position = UDim2.new(0.14, 0, 0.583, 0)

			elseif Num == 3 then
				textLabel.Position = UDim2.new(0.205, 0, 0.219, 0)

			elseif Num == 4 then
				textLabel.Position = UDim2.new(0.599, 0, 0.191, 0)

			end
			textLabel.TextColor3 = Color3.new(0, 0.996078, 0.396078)
			textLabel.TextStrokeColor3 = Color3.new(0.262745, 0.54902, 0.0588235)
			textLabel.Text = "+"..GummyBearCoin
			textLabel.Visible = true
			coinSound:Play()
			Hit.Transparency = 1
			Hit.CanTouch = false
			for _, part in ipairs(Hit:GetChildren()) do
				if part.Name == "Part" then
					part.Transparency = 1
				end
				
			end
			
		
			wait(0.3)
			debounce = false
			wait(1.2)
			textLabel.Visible = false
			textLabel:Destroy()


		end
		

	end
	
	
end)

Thanks :slight_smile:

Is your localscript inside the character? It’ll get deleted when the player respawns. You can try executing it in a new thread with spawn, or just moving the script to the player.

spawn(function()
	wait(1.2)
	textLabel.Visible = false
	textLabel:Destroy()
end)

so i think if the humanoid has beend estroyed the script stops, so a fix would be to wrap the whole thing in a coroutine

make sure the screengui ResetOnSpawn checkbox is uncheck otherwise the gui will appear again when you die

@Denzovorus My localscript is not inside the character, it is inside playerGui.
But, I will still try using spawn()

@Sniperkaos I will try that.

@SingaporeSteadyLah I will try that

That does fix it, except there is another problem, since the localscripts are in the gui, if the player dies, then the +2 doesn’t appear anymore.

You could substitute the wait(1.2) or use both to suit your needs and replace it Debris since it does not yield, so your code should look something like this:

game:GetService("Debris"):AddItem(textLabel,1.2)

I don’t know if this 100% works since I’m not sure if it still continues after death.

Thank you! That works perfectly :+1:

1 Like