Touched event not working correctly

hello, I’m trying to make a powerup script where an item spawns on a timer, but wont spawn if no one has picked up the item. The problem is that the Touched event to pick up the item isnt firing correctly. here is the code: (powerup is a tool)

local powerup = game.ServerStorage.powerup
local debounce = false

while true do
	print("SPEEDBOOST SPAWNED!")
	power = powerup:Clone()
	power.Parent = game.Workspace
	power.powerup.Position = Vector3.new(-59, 27.76, 21.5)
	wait(15)
	while power.Parent == game.Workspace do
		wait(10)
	end
end


power.powerup.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			power.Parent = Player:WaitForChild("Backpack")
		elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
			local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent.Parent)
			power.Parent = Player:WaitForChild("Backpack")
		end
	end
	wait(1)
	debounce = false
end)

The script is in ServerScriptService. Thanks in advance!

It’s because the While Loop will never end, so the script will never make it to that part of the code. Just move the touched event above the while loop.

1 Like

The script will never get to the part with the .touched event and will stay busy unless you set power.Parent to something other than the workspace.