When player died UI doesn't update

Hello! I have this code that create a box for the player and has a CD, if player move or do smth the box will disappear and start the CD. The problem I’m having is that when I use the box and after that the player die, he is not able to use the box again.
I’ll appreciate any help, thanks! :smiley:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local magicBoxUseEvent = ReplicatedStorage:WaitForChild("magicBoxUseEvent")

local player = Players.LocalPlayer
local cooldownTime = 10 -- segundos
local canUseBox = true
local lastActivationTime = 0
local uiCDtext = player:WaitForChild("PlayerGui"):WaitForChild("PowerUps"):WaitForChild("magicBox"):WaitForChild("cdText")


local playerBox = nil
local isBoxActive = false

-- Create box
local function createBox()
	local box = Instance.new("Part")
	box.Size = Vector3.new(6, 6, 6)
	box.Anchored = true
	box.CanCollide = false
	box.BrickColor = BrickColor.new("Bright orange")
	box.Parent = workspace
	return box
end

-- Update CD UI 
local function updateCooldownGui(remainingTime)
	if remainingTime > 0 then
		if uiCDtext.Visible == false then
			uiCDtext.Visible = true
		end
		uiCDtext.Text = string.format("%.1f", remainingTime)
	else
		uiCDtext.Visible = false
	end
end

-- Start cooldown function
local function startCooldown()
	lastActivationTime = tick()  
	canUseBox = false
end

-- Destroy Box
local function destroyBox()
	isBoxActive = false
	if playerBox then
		playerBox:Destroy()
		playerBox = nil
		-- Init cooldown 
		startCooldown()
	end
end

-- Activation Box
local function activateBox()
	local currentTime = tick()

	if canUseBox and (currentTime - lastActivationTime >= cooldownTime) then
		local playerPosition = player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Character.HumanoidRootPart.Position

		if playerPosition then
			playerBox = createBox()
			playerBox.Position = playerPosition
			playerBox.Transparency = 0 -- Do visible
			canUseBox = false
			lastActivationTime = currentTime
			isBoxActive = true

			-- Mov player to Spawn Box Point
			player.Character:SetPrimaryPartCFrame(CFrame.new(playerPosition))

		end
	end
end

-- Manage key F
local function onKeyDown(input, gameProcessed)
	if not gameProcessed and input.KeyCode == Enum.KeyCode.F then
		activateBox()
	else
		destroyBox()
	end
end

-- Connect function to keys 
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	onKeyDown(input, gameProcessed)
end)


-- Update CD status with RenderStepped
local lastIsBoxActive = nil  

RunService.RenderStepped:Connect(function(deltaTime)
	if not canUseBox then
		local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
		if humanoid and humanoid.WalkSpeed > 0 then
			canUseBox = true
		end
	end

	-- Update UI
	if isBoxActive == false  then
		local remainingTime = math.max(0, cooldownTime - (tick() - lastActivationTime))
		updateCooldownGui(remainingTime)
	end
	
	-- Check if isBoxActive is the same value than lastIsBoxActive
	if isBoxActive ~= lastIsBoxActive then
		lastIsBoxActive = isBoxActive

		if isBoxActive then
			player:WaitForChild("PlayerGui"):WaitForChild("PowerUps").isBoxActive.Value = true
			print("Cliente -> Turn isBoxActive TRUE ")
			magicBoxUseEvent:FireServer(true)
		else
			player:WaitForChild("PlayerGui"):WaitForChild("PowerUps").isBoxActive.Value = false
			print("Cliente -> Turn isBoxActive FALSE")
			magicBoxUseEvent:FireServer(false)
		end
	end
end)

It just loose the reference because the script delete when player dies.

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