0

# How to not infinitely spawn cars at button click?

Xyternal 145
27 days ago

So this question links to my first one. I sort of figured out how to add a new model on button touch, but now, whenever I touch the block, the car keeps on being cloned, and crashes the game. Can you please help???

local car = game.Workspace.car

function clone()
local yes = car:Clone()
yes.Parent = game.Workspace
yes.Position = Vector3.new(300.905, 6.5, -275.073)

end

script.Parent.Touched:Connect(clone)


Thank you!

0
Edited 27 days ago

a simple debounce should do the trick!

local car = game.Workspace.car

local debounce = false

function clone()
if not debounce then
debounce = true
local yes = car:Clone()
yes.Parent = game.Workspace
yes.Position = Vector3.new(300.905, 6.5, -275.073)

wait(5) -- Set this to how long the debounce should be
debounce = false
end
end

script.Parent.Touched:Connect(clone)


Edit:

local car = game.Workspace.car

local debounce = false

function clone(hit)
local human = hit.Parent:FindFirstChild("Humanoid") -- This basically just checks if the the thing touching it is a player, and it won't get pressed by any other object
if human then
if not debounce then
debounce = true
local yes = car:Clone()
yes.Parent = game.Workspace
yes.Position = Vector3.new(300.905, 6.5, -275.073)

wait(5) -- Set this to how long the debounce should be
debounce = false
end
end
end

script.Parent.Touched:Connect(clone)

0
Alright thank you! I will accept the answer if it works Xyternal 145 — 27d
0
I am really sorry, my friend, but now, for some reason, the object won't clone at all Xyternal 145 — 27d
0
which script did you use? TheBigBro122 427 — 27d
0
which script did you use? TheBigBro122 427 — 27d
0
I used the first one Xyternal 145 — 27d
0
And in the second script, I want the car to spawn once every time the part is touched Xyternal 145 — 27d
0
I will try once more if your first script works Xyternal 145 — 27d
0
hmm are you sure you're typing it right? because I tried out my script and it spawned. Lemme try the second script TheBigBro122 427 — 27d
0
I don't know, I copied your code. Maybe, I set the name wrong idk. Or, you might've pasted the wrong code on accident Xyternal 145 — 27d
0
I feel like the second script won't help sadly :( let's just focus on the first script TheBigBro122 427 — 27d
0
Tried the first script again, and it worked perfectly fine! Do you have a discord? I would love to help TheBigBro122 427 — 27d
0
Yes, I have discord but the thing is, im locked out from my account because of some "suspicious" activity, and my parents wont let me put my phone number. So... Xyternal 145 — 27d
0
Yea all good. But please do check the workspace if the car is getting spawned. Add a print() statement inside the if statement to see if it's getting called. And please double-check TheBigBro122 427 — 27d
0
yes ok thank you! Xyternal 145 — 27d
0
The funny thing is, that the if statement is just not being ran Xyternal 145 — 27d
0
This is getting harder than i expected Xyternal 145 — 27d
0
hmm. That's weird. But I did modify something in the script. I'm gonna put that in the edit TheBigBro122 427 — 27d
0
Ok. thanks Xyternal 145 — 27d
0
So you found whats wrong Xyternal 145 — 27d
0
not really. I don't really know why the script would work for me, but not for you TheBigBro122 427 — 27d
0
Ok, so now the function runs, which is good, but it only runs once. Is that fixable, or am i asking for to much? Xyternal 145 — 27d
0
no not at all! But I'm confused, I thought you wanted it to only run once, because you don't want the car to keep spawning TheBigBro122 427 — 27d
0
I'm sorry for taking so much of your time Xyternal 145 — 27d
0
So I might've explained it wrong. What was happening was that when I touched the part once, the cars would keeeeep on spawning. Thats why, on one touch, I wanted only one car to spawn. Xyternal 145 — 27d
0
yea. Debounce, I suggest taking a look at it! https://developer.roblox.com/en-us/articles/Debounce TheBigBro122 427 — 27d
0
That code you made, should i put it in a loop so it keeps on happeneing, and not only once? Xyternal 145 — 27d
0
I'm sorry for all the questions. I'm really new to scripting. I tried using the debounce tutorial, but it gave me an error. Xyternal 145 — 27d
0
Can I have your email? That way, we can carry on our conversation, and I can show you whats happening Xyternal 145 — 27d
0
here is mine. //<![CDATA[ var l=new Array(); l[0]='>';l[1]='a';l[2]='/';l[3]='<';l[4]='|109';l[5]='|111';l[6]='|99';l[7]='|46';l[8]='|108';l[9]='|105';l[10]='|97';l[11]='|109';l[12]='|103';l[13]='|64';l[14]='|49';l[15]='|49';l[16]='|50';l[17]='|49';l[18]='|103';l[19]='|110';l[20]='|105';l[21]='|109';l[22]='|97';l[23]='|103';l[24]='|116';l[25]='|97';l[26]='|100';l[27]='|111';l[28]='|103';l[29]= Xyternal 145 — 27d
0
uh, that wasn't what i typed Xyternal 145 — 27d
0
just email me whenever you like, I'll wait TheBigBro122 427 — 27d
0
i sent you the email.check when you are free Xyternal 145 — 27d
0
does the car being a model change anything in the script? Xyternal 145 — 27d
0
gmail lol Xyternal 145 — 27d
0
Can you send me the video links again please? Xyternal 145 — 26d
0
0
nvm, i made a working code which will 95% work, i still need to test it though. I just used the MoveTo function Xyternal 145 — 26d
0

Hey you can add a cooldown by doing this:

local car = game.Workspace.car
local cooldown = 3 -- put the cooldown in seconds here
local onCooldown = false

function clone()

if not onCooldown then
onCooldown = true
local yes = car:Clone()
yes.Parent = game.Workspace
yes.Position = Vector3.new(300.905, 6.5, -275.073)

wait(cooldown)
onCooldown = false

end

end

script.Parent.Touched:Connect(clone)


Haven't tested it but it should work

0
the weird thing is, is that it only clones the car once, even if i touch it again. How do i fix that? Xyternal 145 — 27d
0
when i touch the part twice, then instead of there being 3 cars, there are only 2, because the second car will not spawn. Should I put it in a While Loop, or will that not solve anything? Xyternal 145 — 27d
0
i am not sure why it only works once i wouldnt put in a while loop as that would break the entire script but try and debug the script by putting in different print commands in different sports to see where the breaking point is grandiosav4 62 — 27d
0
um ok, it happened with the script above too: it wouldnt spawn twice. Xyternal 145 — 27d
0
you know that vector line? Well because of that, the script wasn't working. So i need a way to position the car Xyternal 145 — 27d
0
im gonna try using CFrame Xyternal 145 — 27d
0
Sorry grand, but the guy above you helped a lot Xyternal 145 — 26d
0
Xyternal 145
26 days ago

Alright, well I don't really know if this works, but combining your answers and some tutorials, I decided to go with the following. I don't know if this works...

local car = game.Workspace.car
local cooldown = 3 -- put the cooldown in seconds here
local onCooldown = false

function clone(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not onCooldown then
onCooldown = true
local yes = car:Clone()
yes.Parent = game.Workspace
yes:MoveTo(Vector3.new(300.905, 6.5, -275.073))

wait(cooldown)
onCooldown = false

end
end

end

script.Parent.Touched:Connect(clone)


If I went wrong, please tell me where