Item spawning twice?

The code that is below is inside a script that is inside a part. All of it works, but I am still confused about why it is spawning 2 items and the button cannot be pressed multiple times to spawn more than once.

local myBrick = script.Parent

local function imTriggered(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then 
		print ("Human")
		local Sound = game.Workspace.Sound
		Sound:Destroy()
		local Segway = game.ServerStorage.SegwayKit
		local Segway2 = Segway:Clone()
		Segway2.Name= "Segway2"
		Segway2.Parent= workspace
				
		

	end
end

myBrick.Touched:Connect(imTriggered)

I’d try adding a DeBounce to this script. The player can activate .touched events incredibly quickly

.Touched event is unstable and can call a function multiple times rather than once. I’d suggest you adding a debounce (variable = true/false).

1 Like

It’s probably because sound:destroy() can only be called once since after that there isn’t a sound to destroy. Maybe try doing sound:stop()

1 Like

That’s not related to the spawning, I just put that in there for learning to use the Destroy() method

Alright I changed it to this and it’s still having the same two problems:

local buttonPressed = false
local myBrick = script.Parent

if not buttonPressed then
	
	buttonPressed=true
	 function imTriggered(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then 
		print ("Human")
		local Sound = game.Workspace.Sound
		Sound:Destroy()
		local Segway = game.ServerStorage.SegwayKit
		local Segway2 = Segway:Clone()
		Segway2.Name= "Segway2"
			Segway2.Parent= workspace
			buttonPressed = false
				
		

	end
	end
	
end


myBrick.Touched:Connect(imTriggered)

You could add a wait() function, or use RemoteEvents to fire when the player’s magnitude is further away from the part then set the variable to false.

1 Like

I’d add it as a comment then because if the script errors then the touched event won’t run.

Instead of doing if not buttonpressed, change it to if buttonpressed == false

@SuperCryptic9 Okay I will do that
@Oseuka Isn’t that the same thing though?

Yeah it is the same thing. The debounce thing will work, it just needs to be inside the imtriggered() function, right next to where you check if humanoid.

oml what happened lol
image


local buttonPressed = false
local myBrick = script.Parent

if not buttonPressed then
	
	buttonPressed=true
	 function imTriggered(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then 
		print ("Human")
--		local Sound = game.Workspace.Sound
--	Sound:Destroy()
		local Segway = game.ServerStorage.SegwayKit
		local Segway2 = Segway:Clone()
		Segway2.Name= "Segway2"
			Segway2.Parent= workspace
			buttonPressed = false
				
		

	end
	end
	
end


myBrick.Touched:Connect(imTriggered)

If you only need to use the script once then I’d just destroy the script lol

I’m just using this in my scripting test world to test it out, so I would want to keep it in case I need something like it later

1 Like

A post was merged into an existing topic: Feedback - Bypassing the 30 Character Limit

I don’t know if you misunderstood, but I meant putting script:Destroy() once the script is done doing what it needs to do. I didn’t mean delete the script, just destroying it in the game to stop it from running more than once.

Although I’m surprised the DeBounce isn’t working.

Alright the item is still being spammed with this script what’s wrong? It seems like I did everything right?


local buttonPressed = false
local myBrick = script.Parent

if not buttonPressed then
	
	
	 function imTriggered(part)
		buttonPressed=true
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then 
		print ("Human")
--		local Sound = game.Workspace.Sound
--	Sound:Destroy()
		local Segway = game.ServerStorage.SegwayKit
		local Segway2 = Segway:Clone()
		Segway2.Name= "Segway2"
			Segway2.Parent= workspace
			wait(5)
			buttonPressed = false
				
		

	end
	end
	
end


myBrick.Touched:Connect(imTriggered)

Then you should see that the order of items is important:

function
if debounce
change debounce
do stuff
wait
change debounce back
end

you have your debounce before the function

Oh I see I want this to be an item spawner, so I never want it to be destroyed.