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

Why is this Vehicle Script not Working?

Asked by 10 years ago

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

Log in to vote
1
Answered by 10 years ago

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
0
Is a local script needed for this to work? CoolJohnnyboy 121 — 10y
0
No, it can be used in a normal script :) DevJackB 55 — 10y
Ad
Log in to vote
0
Answered by
Mr_Octree 101
10 years ago

Break the joints of the car model as well.

0
That's what script.Parent.Parent is, and it's not working. CoolJohnnyboy 121 — 10y
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. Mr_Octree 101 — 10y
0
The BreakJoints() thing is for the sake of having it look decent. After is breaks, it gets removed. CoolJohnnyboy 121 — 10y
0
You could try using a for loop to remove any welds within the parts of the vehicle. Mr_Octree 101 — 10y
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. CoolJohnnyboy 121 — 10y

Answer this question