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

How to not infinitely spawn cars at button click?

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!

3 answers

✓ Accepted answer

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)

37 comments

0

Alright thank you! I will accept the answer if it works

0

I am really sorry, my friend, but now, for some reason, the object won't clone at all

0

which script did you use?

0

which script did you use?

0

I used the first one

0

And in the second script, I want the car to spawn once every time the part is touched

0

I will try once more if your first script works

0

hmm are you sure you're typing it right? because I tried out my script and it spawned. Lemme try the second script

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

0

I feel like the second script won't help sadly :( let's just focus on the first script

0

Tried the first script again, and it worked perfectly fine! Do you have a discord? I would love to help

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...

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

0

yes ok thank you!

0

The funny thing is, that the if statement is just not being ran

0

This is getting harder than i expected

0

hmm. That's weird. But I did modify something in the script. I'm gonna put that in the edit

0

Ok. thanks

0

So you found whats wrong

0

not really. I don't really know why the script would work for me, but not for you

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?

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

0

I'm sorry for taking so much of your time

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.

0

yea. Debounce, I suggest taking a look at it! https://developer.roblox.com/en-us/articles/Debounce

0

That code you made, should i put it in a loop so it keeps on happeneing, and not only once?

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.

0

Can I have your email? That way, we can carry on our conversation, and I can show you whats happening

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]=

0

uh, that wasn't what i typed

0

just email me whenever you like, I'll wait

0

i sent you the email.check when you are free

0

does the car being a model change anything in the script?

0

gmail lol

0

Can you send me the video links again please?

0

pleaseeeee

0

nvm, i made a working code which will 95% work, i still need to test it though. I just used the MoveTo function

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

7 comments

0

the weird thing is, is that it only clones the car once, even if i touch it again. How do i fix that?

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?

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

0

um ok, it happened with the script above too: it wouldnt spawn twice.

0

you know that vector line? Well because of that, the script wasn't working. So i need a way to position the car

0

im gonna try using CFrame

0

Sorry grand, but the guy above you helped a lot

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

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