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

Help With Car Generator Script?

local model = game.Lighting.Car
local character = game.Players.LocalPlayer.Character
local ifcar = game.Players.LocalPlayer.leaderstats.Car.Value
local a = model:clone()

function onClicked()
if ifcar == 0 then
a.Parent = game.Workspace
wait()
a:MakeJoints()
wait()
a:MoveTo(character.Torso.Position + Vector3.new(0,0,10))
ifcar = 1
elseif ifcar == 1 then
	a:remove()
	ifcar = 0
	end
end
script.Parent.MouseButton1Click:connect(onClicked)

I have this script that mostly works fine, the only problem is that once the persons removes the car by clicking the gui again, they can't get the car back anymore. I know what the issue is, that the script can only create the clone once, but I don't know how to fix it. I can put the clone command inside the function, but then that stops the function from getting rid of the car.

Someone please help?

1 answer

✓ Accepted answer

Well simply move the line that clones the car into the function itself. That way it will clone it every time it is clicked or when "ifcar==0".

local model = game.Lighting.Car
local character = game.Players.LocalPlayer.Character
local ifcar = game.Players.LocalPlayer.leaderstats.Car


function onClicked()
if ifcar .Value== 0 then
a = model:clone()
a.Parent = game.Workspace
wait()
a:MakeJoints()
wait()
a:MoveTo(character.Torso.Position + Vector3.new(0,0,10))
ifcar.Value = 1
elseif ifcar .Value== 1 then
    a:Destroy()
    ifcar.Value = 0
    end
end
script.Parent.MouseButton1Click:connect(onClicked)

Btw, I had to make a few other changes. First off, you just put the current value of Car into a variable, if you tried to change that variable, that is all it would do. It would only change the direct variable and not the Value that is in Car. Instead, put the Name of the Value into a variable, not the value. Then go variable.Value.

Second, use :Destroy(), not :remove(). Remove works the same way as a "cut" command. Even though you removed the part, it is still floating in the memory and can still be used. This means it is taking up space. :Destroy() will delete it permanently and free up space on a server or client.

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