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

Why is this Vehicle Script not Working?

So this script is supposed to make a vehicle's Parent be your character, and if you die, it will break the vehicle and remove it.

The script makes you the parent of the vehicle, but it won't break or get removed after you die. Can someone please help me?

Here's the script:

function onDie()
	human = nil
	script.Parent.Parent:BreakJoints()
	wait(3)
	script.Parent.Parent:remove()
end

function Child(c)
	if c.Name == "SeatWeld" and c.Part1.Name == "Torso" then
		human = c.Part1.Parent
		script.Parent.Parent.Parent = human
		human.Humanoid.Died:connect(onDie)
	end
end

function noChild()
	human = nil
	script.Parent.Parent.Parent = game.Workspace
end

script.Parent.ChildAdded:connect(Child)
script.Parent.ChildRemoved:connect(noChild)

2 answers

As per a comment above, you could use a for loop to remove welds instead of using the BreakJoints() method.

Also, PLEASE don't use the Remove() method. Instances that are removed rather than destroyed don't get added to the garbage collection list, and this can affect gameplay.

Example:

local car = script.Parent

local function onDeath()
	human = nil
	local parts = car:GetChildren()
	for i = 1, #parts do
		if parts[i]:IsA("Weld") then
			parts[i]:Destroy()
		end
	end
	car:Destroy()
end

local function child(c)
	if c.Name == "SeatWeld" and c.Part1.Name == "Torso" then
		human = c.Part1.Parent
		script.Parent.Parent.Parent = human
		human.Humanoid.Died:connect(onDeath)
	end
end

local function noChild()
	human = nil
	script.Parent.Parent.Parent = game.Workspace
end

2 comments

0

Is a local script needed for this to work?

0

No, it can be used in a normal script :)

Break the joints of the car model as well.

5 comments

0

That's what script.Parent.Parent is, and it's not working.

0

Maybe the vehicle is welded together. Why not just destroy the whole car model? The BreakJoints() method of destroying cars is a bit outdated and messier.

0

The BreakJoints() thing is for the sake of having it look decent. After is breaks, it gets removed.

0

You could try using a for loop to remove any welds within the parts of the vehicle.

0

The vehicle didn't become a child of the player. Weird thing is, the regen button I had created a new vehicle, which it normally wouldn't do because of this script.

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