Issue with Individual Cooldown Timers in ProximityPrompt

Hello everyone,

I’m currently working on a script for a “Lucky Block” feature in my game, where players can receive a Lucky Block by a npc called “Emilia” by interacting with a ProximityPrompt. I want each player to have their own individual cooldown timer and only see their own cooldown, but I’m encountering an issue.

The Problem:

I am using a ProximityPrompt to let players interact with a Lucky Block and receive it. However, I want each player to see their own cooldown (how much time they need to wait before interacting again) and not the cooldown of other players.

Currently, the script works, but when one player interacts with the Lucky Block, the cooldown timer is updated globally, meaning all players can see the same cooldown, and the text displayed in the ProximityPrompt is the same for everyone. This causes confusion because players can see each other’s cooldowns also when there is more than 1 player everyone has to take the luckyblock for the cooldown to start.

Before taking the lucky block:

After taking the lucky block: (cooldown doesn’t start)

After all players took the lucky block: (Cooldown starts)

NOTE: THE NPC CALLED “Emilia” HAVE A PART NAMED “LuckyBlock” With the ProximityPromt

THE SCRIPT:

– Script inside Emilia → LuckyBlock
local ProximityPrompt = script.Parent:FindFirstChild(“ProximityPrompt”)
local cooldownTime = 15 * 60 – 15 minutes in seconds
local playerCooldowns = {} – Table to track cooldowns for each player

– Function to give the lucky block and set the cooldown for the player
local function giveLuckyBlock(player)
local currentTime = tick()

-- Get the player's individual cooldown, or set it to 0 if it doesn't exist
local lastGiveTime = playerCooldowns[player.UserId] or 0

-- Check if the player's cooldown is still active
if currentTime - lastGiveTime < cooldownTime then
	-- If the cooldown is active, do nothing
	return
end

-- Update the last used time for the player
playerCooldowns[player.UserId] = currentTime

-- Create and add the lucky block to the player's backpack
local luckyBlock = game.ServerStorage:FindFirstChild("Lucky")
if luckyBlock then
	luckyBlock:Clone().Parent = player.Backpack

	-- Notify the player
	local playerGui = player:FindFirstChild("PlayerGui")
	if playerGui then
		local textLabel = playerGui:FindFirstChild("ScreenGui"):FindFirstChild("TextLabel")
		if textLabel then
			textLabel.Text = "You have received a lucky block!"
		end
	end
else
	warn("LuckyBlock not found in ServerStorage.")
end

end

– Function to update the cooldown text on the ProximityPrompt
local function updateCooldownText()
while true do
for _, player in ipairs(game.Players:GetPlayers()) do
local lastGiveTime = playerCooldowns[player.UserId] or 0
local currentTime = tick()
local timeSinceLastGive = currentTime - lastGiveTime
local timeRemaining = math.max(0, cooldownTime - timeSinceLastGive)

		-- Show the remaining time on the ProximityPrompt for each player
		if timeRemaining > 0 then
			local minutes = math.floor(timeRemaining / 60)
			local seconds = math.floor(timeRemaining % 60)
			ProximityPrompt.ActionText = string.format("Wait %02d:%02d to interact", minutes, seconds)
		else
			-- Reset the text when the cooldown is over
			ProximityPrompt.ActionText = "Press [E] to get a Lucky Block"
		end
	end
	wait(1)  -- Update every second
end

end

– Connect the function to the ProximityPrompt if it exists
if ProximityPrompt then
ProximityPrompt.Triggered:Connect(giveLuckyBlock)
spawn(updateCooldownText) – Start the cycle to update the cooldown text
else
warn(“ProximityPrompt not found in the Emilia model.”)
end

What I want:

  • Each player should have their own individual cooldown timer that is shown only to them.
  • The ProximityPrompt’s action text should reflect the cooldown time for that specific player, not for everyone.
  • I want the cooldown text to update as soon as any player uses the Lucky Block, not just when all players have interacted with it.

What I’ve tried:

  • I attempted to use individual ProximityPrompts for each player, but the cooldown text was still visible to all players.
  • I’m storing cooldown times in a table to track when each player last interacted, but it doesn’t seem to prevent others from seeing the cooldown.

Any suggestions on how to implement this properly? I need help ensuring each player has their own ProximityPrompt with a separate cooldown timer visible only to them.

Thanks for your help!

1 Like

I’d recommend using remote events and local scripts so the effects of picking up a lucky block only appears for the player that activated the prompt

I figured out a system using this appraoch, check it out:
Explorer:
Screenshot 2024-11-10 152902

Note: the object under ReplicatedStorage is a RemoteEvent named RequestLuckyBlock


LuckyBlockServer script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LuckyBlockRequest = ReplicatedStorage:WaitForChild("RequestLuckyBlock")

local ProximityPrompt = script.Parent:FindFirstChild("ProximityPrompt")

if ProximityPrompt then
	ProximityPrompt.Triggered:Connect(function(plr)
		LuckyBlockRequest:FireClient(plr,ProximityPrompt)
	end)
else
	warn("ProximityPrompt not found in the Emilia model.")
end

LuckyBlockRequest.OnServerEvent:Connect(function(player)
	local luckyBlock = game.ServerStorage:FindFirstChild("Lucky")
	if luckyBlock then
		luckyBlock:Clone().Parent = player.Backpack

		-- Notify the player
		local playerGui = player:FindFirstChild("PlayerGui")
		if playerGui then
			local textLabel = playerGui:FindFirstChild("ScreenGui"):FindFirstChild("TextLabel")
			if textLabel then
				textLabel.Text = "You have received a lucky block!"
			end
		end
	else
		warn("LuckyBlock not found in ServerStorage.")
	end
end)

LuckyBlockClient script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LuckyBlockRequest = ReplicatedStorage:WaitForChild("RequestLuckyBlock")
local ProximityPrompt = workspace.Emilia.LuckyBlock.ProximityPrompt

local lastRequestTime = 0

local cooldownTime = 15 * 60 -- 15 minutes

LuckyBlockRequest.OnClientEvent:Connect(function()
	if lastRequestTime then
		if tick() - lastRequestTime > cooldownTime then
			lastRequestTime = tick()
			
			LuckyBlockRequest:FireServer()
			
			print("block given!")
		else
			print("cooling down!")
		end
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	local timeSinceLastGive = tick() - lastRequestTime or 0
	local timeRemaining = math.max(0, cooldownTime - timeSinceLastGive)
	
	if timeRemaining > 0 then
		local minutes = math.floor(timeRemaining / 60)
		local seconds = math.floor(timeRemaining % 60)
		ProximityPrompt.ActionText = string.format("Wait %02d:%02d to interact", minutes, seconds)
	else
		-- Reset the text when the cooldown is over
		ProximityPrompt.ActionText = "Press [E] to get a Lucky Block"
	end
end)

Try it out and let me know if you have any issues!

1 Like

Tysm man, I’ve did everything u told me and everything works perfectly!

1 Like

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