Help Creating Power-Up (Mystery Box) System

I’m looking to create a power-up (mystery box) system where players receive random power-ups.

  1. Random Power-up: When a player interacts with a box, they receive a random power-up.
  2. Avoid Duplicate Power-ups: If the player receives a different power-up than what they currently have, nothing happens. Only if they get the same power-up again, the timer resets.
  3. Example: If a player gets a “2x speed” power-up from one box, and then gets a “2x jump” power-up from another box, the “2x speed” effect should remain. If the player encounters another “2x speed” power-up, the timer for “2x speed” should restart.

I can do this by maintaining a record of the current power-up the player has and its duration. When a player interacts with a box, generate a random power-up. If it’s different from the current one, do nothing. If it’s the same, reset the timer.

Problem: I don’t know how to do this :frowning:

when i get a power up, check the current power I have, and the time remaining.

If CurrentPower == "Power1" and CurrentTime > 0 then
--Reset time
end
If CurrentPower == "Power2" and CurrentTime > 0 then
--Reset time
end

I had to modify it some but I made this

Problem: As you see in the video, the loop of the timer gets mest up and I don’t know how to fix it. The way it looks is that the player receives more then one power-up but the code prevents that in this block of code.

-- Apply the power-up to the player if it matches their current power-up or if they have no current power-up
if Chosen == Player.PowerUp.Value then
	Player.PowerUp.Value = Chosen
	Reset = 1  -- Set flag to reset power-up duration
elseif Player.PowerUp.Value == "None" then
	Player.PowerUp.Value = Chosen
end

Code:

-- Services and RemoteEvents
local CollectionService = game:GetService("CollectionService")
local PowerUpEvent = game.ReplicatedStorage.Events.PowerUpEvent

-- Define power-ups and initialize variables
local PowerUps = {"Speed", "Jump"}
local Collected = {}
local PowerUpTime = 20
local Reset = 0

-- Iterate through parts tagged as "PowerUpBox"
for i, Part in pairs(CollectionService:GetTagged("PowerUpBox")) do
	-- Connect a function to the Touched event of each part
	Part.Touched:Connect(function(Hit)
		-- Check if the touched object has a Humanoid and if the power-up box has not been collected yet
		if Hit.Parent:FindFirstChild("Humanoid") and Collected[Part] == nil then
			Collected[Part] = true  -- Mark the power-up box as collected

			-- Get the player who touched the power-up box
			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

			-- Choose a random power-up from the list
			local Chosen = PowerUps[math.random(1, #PowerUps)]
			print(Player.Name .. " received " .. Chosen)

			-- Apply the power-up to the player if it matches their current power-up or if they have no current power-up
			if Chosen == Player.PowerUp.Value then
				Player.PowerUp.Value = Chosen
				Reset = 1  -- Set flag to reset power-up duration
			elseif Player.PowerUp.Value == "None" then
				Player.PowerUp.Value = Chosen
			end

			-- Activate the power-up effect if the chosen power-up matches the player's current power-up
			if Player.PowerUp.Value == Chosen then
				while PowerUpTime > 0 do
					
					-- Reset power-up duration if flagged
					if Reset == 1 then
						PowerUpTime = 20
						Reset = 0
					end
					
					-- Notify the player of the remaining time for the power-up
					PowerUpEvent:FireClient(Player, "2x " .. Chosen .. ": " .. PowerUpTime)
					task.wait(1)  -- Wait for 1 second
					PowerUpTime -= 1  -- Decrease remaining time
				end
				
				-- Reset player's power-up after duration ends
				Player.PowerUp.Value = "None"
				PowerUpEvent:FireClient(Player, "Disable")  -- Notify client to disable power-up effect
			end

			-- Reset variables and removes power-up box for tabel
			PowerUpTime = 20
			Collected[Part] = nil
		end
	end)
end

I found another problem. When the player touches the power-up box, they get a power-up, and the countdown starts. If you look at the output, the player received the power-up, and when they receive another one, the countdown resets to 20.

Problem: When the player touches more than one power-up box, the countdown restarts, but when the countdown is done, it prints the player received power-up and starts a new countdown as if the player just touched the power-up box.

Output: Player touching only two power-up boxes

  08:52:56.381  CryptoHex7 received Jump  -  Server - Script:24
  08:52:56.381  CryptoHex7 remaining time for Jump: 20  -  Server - Script:45
  08:52:57.382  CryptoHex7 remaining time for Jump: 19  -  Server - Script:45
  08:52:58.398  CryptoHex7 remaining time for Jump: 18  -  Server - Script:45
  08:52:58.798  CryptoHex7 received Speed  -  Server - Script:24
  08:52:58.799  Resetting variables and removing power-up box.  -  Server - Script:57
  08:52:58.898  CryptoHex7 received Jump  -  Server - Script:24
  08:52:58.899  CryptoHex7 remaining time for Jump: 20  -  Server - Script:45
  08:52:59.415  CryptoHex7 remaining time for Jump: 19  -  Server - Script:45
  08:52:59.916  CryptoHex7 remaining time for Jump: 18  -  Server - Script:45
  08:53:00.416  CryptoHex7 remaining time for Jump: 17  -  Server - Script:45
  08:53:00.932  CryptoHex7 remaining time for Jump: 16  -  Server - Script:45
  08:53:01.432  CryptoHex7 remaining time for Jump: 15  -  Server - Script:45
  08:53:01.948  CryptoHex7 remaining time for Jump: 14  -  Server - Script:45
  08:53:02.449  CryptoHex7 remaining time for Jump: 13  -  Server - Script:45
  08:53:02.949  CryptoHex7 remaining time for Jump: 12  -  Server - Script:45
  08:53:03.465  CryptoHex7 remaining time for Jump: 11  -  Server - Script:45
  08:53:03.964  CryptoHex7 remaining time for Jump: 10  -  Server - Script:45
  08:53:04.466  CryptoHex7 remaining time for Jump: 9  -  Server - Script:45
  08:53:04.981  CryptoHex7 remaining time for Jump: 8  -  Server - Script:45
  08:53:05.482  CryptoHex7 remaining time for Jump: 7  -  Server - Script:45
  08:53:05.998  CryptoHex7 remaining time for Jump: 6  -  Server - Script:45
  08:53:06.498  CryptoHex7 remaining time for Jump: 5  -  Server - Script:45
  08:53:07.015  CryptoHex7 remaining time for Jump: 4  -  Server - Script:45
  08:53:07.514  CryptoHex7 remaining time for Jump: 3  -  Server - Script:45
  08:53:08.031  CryptoHex7 remaining time for Jump: 2  -  Server - Script:45
  08:53:08.530  CryptoHex7 remaining time for Jump: 1  -  Server - Script:45
  08:53:09.048  Resetting variables and removing power-up box.  -  Server - Script:57
  08:53:09.531  CryptoHex7 remaining time for Jump: 19  -  Server - Script:45
  08:53:10.548  CryptoHex7 remaining time for Jump: 18  -  Server - Script:45
  08:53:11.548  CryptoHex7 remaining time for Jump: 17  -  Server - Script:45
  08:53:12.564  CryptoHex7 remaining time for Jump: 16  -  Server - Script:45
  08:53:13.581  CryptoHex7 remaining time for Jump: 15  -  Server - Script:45
  08:53:14.598  CryptoHex7 remaining time for Jump: 14  -  Server - Script:45
  08:53:15.613  CryptoHex7 remaining time for Jump: 13  -  Server - Script:45
  08:53:16.630  CryptoHex7 remaining time for Jump: 12  -  Server - Script:45
  08:53:17.646  CryptoHex7 remaining time for Jump: 11  -  Server - Script:45
  08:53:18.664  CryptoHex7 remaining time for Jump: 10  -  Server - Script:45
  08:53:19.680  CryptoHex7 remaining time for Jump: 9  -  Server - Script:45
  08:53:20.697  CryptoHex7 remaining time for Jump: 8  -  Server - Script:45
  08:53:21.713  CryptoHex7 remaining time for Jump: 7  -  Server - Script:45
  08:53:22.729  CryptoHex7 remaining time for Jump: 6  -  Server - Script:45
  08:53:23.730  CryptoHex7 remaining time for Jump: 5  -  Server - Script:45
  08:53:24.746  CryptoHex7 remaining time for Jump: 4  -  Server - Script:45
  08:53:25.762  CryptoHex7 remaining time for Jump: 3  -  Server - Script:45
  08:53:26.780  CryptoHex7 remaining time for Jump: 2  -  Server - Script:45
  08:53:27.796  CryptoHex7 remaining time for Jump: 1  -  Server - Script:45
  08:53:28.812  Resetting variables and removing power-up box.  -  Server - Script:57
1 Like

Why are you firing the PowerUpEvent every 1 second? Fire it when you touch the part, and the local script should change the time 1 by 1. If it receives another event, that means a new PowerUp, or you need to reset the time.

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