Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would you make a vehicle spawn?

Asked by
emrek 7
5 years ago

I am new to scripting so this might be pretty easy to do but I made a script that when you walk on a button it spawns a car, however the only problem is that if you don't move the car then walk on the spawn button again it will spawn inside of it and glitch it out. So would anyone know how to make a script to avoid this from happening

0
What code do you have so far? iiBuilder_Boy 27 — 5y

2 answers

Log in to vote
0
Answered by
Joshument 110
5 years ago

Most people will usually make it so the car will get destroyed when you press the button, to do this, I would give the car a second GLOBAL VARIABLE after it gets cloned and put in the proper place.

Now what is a global variable you may ask? Well, a global variable is a variable that is defined in the core of the script, like when you get the player service, or define a part at the beginning of a script. What you may not know about these, though, is that you can make them have no value, like this:

local OldCar

This will make a new variable called OldCar with no value. Why is this important, you may ask? Well, setting the clone you made to OldCar will make it so that there is a car you can, well, destroy after you spawn the next one in, so at the top of your script, make a line to destroy the car, as so:

OldCar:Destroy()

This should make it so that the old car will be destroyed when you spawn the new one in. I hope this helped!

0
this is a little helpful but the thing is if someone is driving the car then all of the sudden it will just disappear so I don't want the car to get destroyed I just want it to not spawn when there is a car currently there emrek 7 — 5y
Ad
Log in to vote
0
Answered by
emrek 7
5 years ago

heres the script it is located inside the regen button

model = script.Parent.Parent
regen = script.Parent--Indicates that the script interacts with the model the button is grouped with.

backup = model:clone()
enabled = true

function regenerate()

    model = backup:clone()
    model.Parent = game.Workspace
    model:makeJoints()

    script.Disabled = true
    script.Parent.BrickColor = BrickColor.new(26)--Black
    wait(10)--Change this number to change the time in between regenerations via the button, in seconds..
    script.Parent.BrickColor = BrickColor.new(104)--Purple
    script.Disabled = false
end

function onHit(hit)
    if (hit.Parent:FindFirstChild("Humanoid") ~= nil) and enabled==true then
        regenerate()
    end
end

script.Parent.Touched:connect(onHit)

Answer this question