Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

This script runs twice every time I click the part. Is there any way to work this out?

I am making a tycoon and the first Object that you can buy is a Click Dropper, but the script that I made will make two Parts instead of one whenever I click the button that I have the ClickDetector in.


script.Parent.ClickPart.ClickDetector.MouseClick:connect(function()
	if script.Parent.ClickPart.Transparency == 0 then
		local Part = Instance.new('Part', script.Parent.Parent.Parent.DropParts)
		Part.Size = Vector3.new(1,1,1)
		Part.FormFactor = Enum.FormFactor.Symmetric
		Part.BrickColor = BrickColor.new(255,255,255)
		Part.Position = script.Parent.DropPart.Position
		Part.Anchored = false
		Part.CanCollide = true
		Part.TopSurface = Enum.SurfaceType.Smooth
		Part.BottomSurface = Enum.SurfaceType.Smooth
		local Cash = Instance.new('IntValue', Part)
		Cash.Name = 'Cash'
		Cash.Value = 1
	end
end)

Anything would be very appreciated.

1 answer

Simply check if the part has been clicked with a bool.

local noclick = false

script.Parent.ClickPart.ClickDetector.MouseClick:connect(function()
	if noclick == false then -- checks if the button has be clicked already
		if script.Parent.ClickPart.Transparency == 0 then
			noclick = true -- Part has been clicked and triggered
			local Part = Instance.new('Part',script.Parent.Parent.Parent.DropParts)
			Part.Size = Vector3.new(1,1,1)
			Part.FormFactor = Enum.FormFactor.Symmetric
			Part.BrickColor = BrickColor.new("White") -- Why are you using Color3 values?
			Part.Position = script.Parent.DropPart.Position -- Parts are unanchored and can collide by default
			Part.TopSurface = Enum.SurfaceType.Smooth
			Part.BottomSurface = Enum.SurfaceType.Smooth
			local Cash = Instance.new('IntValue', Part)
			Cash.Name = 'Cash'
			Cash.Value = 1
			wait(0.1) -- Waits 0.1 seconds after the rest of the script has been executed.
			noclick = false -- You can click again
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.