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