Hi, I'm trying to make a clickable button that will respawn a car when you click on it.

Edit: The problem was that it was spawning one every time it saw that a car was far enough away.

Button = script.Parent.ClickDetector

Debounce = true

Spawnable = true

Button.MouseClick:connect(function()

	if Debounce == true then
		
		Debounce = false

	for i,v in pairs(workspace:GetChildren()) do
		
		if v.Name == "Car" or v.Name == "Car2" or v.Name == "Car3" then
			
			if v.Name == "Car" and (v.VehicleSeat.Position - Button.Parent.Position).magnitude > 20 then

				Spawnable = true
			
			elseif (v.VehicleSeat.Position - Button.Parent.Position).magnitude <= 20 then
				
				Spawnable = false				
				
				break
				
			end
			
		end			
		
	end
	
	if not workspace:FindFirstChild("Car") then

		Spawnable = true

	end

	if Spawnable == true then
	
		game.ReplicatedStorage.Car:Clone().Parent = workspace
			
		Spawnable = false
		
		Button.Parent.BrickColor = BrickColor.new("Black")
		
	end
		
	wait(2)	
	
	Debounce = true
	
	Button.Parent.BrickColor = BrickColor.new("Lavender")
	
	end
	
end)

Thanks

2 comments

0

I would suggest you delete this question or answer it yourself and click accept, Just putting solved doesn't cut it.

0

alright