Trying to make it so you can't have more than one powerup at once by parenting objects

Im trying to make it so that powerups can only be picked up by users who have no powerups active by putting an object in the player’s character and having the powerups check if it’s there. There’s a catch: I’m using Instance.new(). This is because adding attributes to the humanoid (using a StarterCharacter) didn’t work, and parenting a value to the character and changing the value didn’t work either.
Do y’all got suggestions on how to fix this issue, or is it time for me to redesign the script?

local Dbounce = 0
local Particles = script.Parent.ParticleEmitter
local Light = script.Parent.PointLight
local boostPart = script.Parent
local BOOSTED_SPEED = 40
local SpeedBoostPlayer = game.ReplicatedStorage:WaitForChild("SpeedBoostPlayer")

local function onPartTouch(hit)
	local partParent = hit.Parent
	local Humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if Dbounce == 0 then
		print("Fired A")
		if Humanoid then
			print("Fired B")
			local Empowered = Instance.new("BoolValue")
			if Empowered.Parent ~= Humanoid then
				Empowered.Parent = Humanoid
				print("Fired C")
				local currentWalkSpeed = Humanoid.WalkSpeed
				local Player = game:GetService("Players"):GetPlayerFromCharacter(partParent)
				Dbounce = 1
				SpeedBoostPlayer:FireClient(Player)
				Particles.Parent = partParent.UpperTorso
				Humanoid.WalkSpeed = BOOSTED_SPEED
				boostPart.Transparency = 1
				Light.Enabled = false
				print("HasteUsed")
				wait(25)
				Humanoid.WalkSpeed = currentWalkSpeed
				Particles.Parent = boostPart
				Particles.Enabled = false
				Empowered:Destroy()
				print("HasteRanOut")
				wait(60)
				Particles.Enabled = true
				boostPart.Transparency = 0.4
				Light.Enabled = true
				Dbounce = 0
				print("HasteRespawned")
			else
				print("Not found")
			end
		end
	end
end

boostPart.Touched:Connect(onPartTouch)

I’ll give more info if it is requested.
(As of posting this, I am going to bed. I’ll add more info if needed tomorrow.)

You’re just putting the BoolValue (to indicate the player has a power-up im guessing) into the humanoid, but without checking if there already is one.

1 Like