Hey guys, newbie scripter here. I'm making a cooking sandbox and stressful type of game, and I need someone to help with this simple script:
function CreatePlate(Position) local Plate = Instance.new("Part") Plate.Position = Position Plate.Size = Vector3.new(4, 0.25, 4) Plate.Color = Color3.new(0.944961, 0.94493, 0.944945) Plate.Parent = workspace task.wait(8) end script.Parent.ClickDetector.MouseClick:Connect(function() CreatePlate(Vector3.new(50, 0, 50)) end)
The create plate function works fine, but it has no cooldown on creating plates even if I put a task.wait(8) in my script. Can someone explain? It will be greatly appreciated!
whenever you have a function in :Connect, it will always run apart from other code, your task.wait(8) won't affect rest of the code. what you want to do is called debounce, you will create a variable and whenever you press ClickDetector, you will set it to true, then after 8 seconds you will set it back to false, and the code will only create the plate if the variable is false.
local CreatePlateDebounce = false function CreatePlate(Position) local Plate = Instance.new("Part") Plate.Position = Position Plate.Size = Vector3.new(4, 0.25, 4) Plate.Color = Color3.new(0.944961, 0.94493, 0.944945) Plate.Parent = workspace end script.Parent.ClickDetector.MouseClick:Connect(function() -- not CreatePlateDebounce is same as CreatePlateDebounce == false in this case if not CreatePlateDebounce then CreatePlateDebounce = true CreatePlate(Vector3.new(50, 0, 50)) task.wait(8) CreatePlateDebounce = false end end)
now to demonstrate that :Connect does not affect rest of the code you can put a while true do loop in it:
script.Parent.ClickDetector.MouseClick:Connect(function() while true do task.wait(0.1) print("Hello, World!") end end)
after clicking more and more times you will notice your output get more and more flooded by Hello Worlds since all these :Connects are running together but apart from each other.