I have a script to Anchor/Unanchor a tank-like vehicle track sections when a player sits on or jumps off a seat. Each track tread is a MeshPart and there are 80 treads, 40 per side. The script is named ‘TrackAnchor’ inside the VehicleSeat in the Screenshot below.
The model is in the workspace with the VehicleSeat and all 80 treads Anchored while the game is played. When a player sits on the seat I want those Parts Unanchored so the vehicle can move.
The issue is that when the player first jumps on the VehicleSeat one of the treads stays Anchored.
It’s always the red coloured tread in the screenshot below, and when I test that red one is always the first in the Explorer window. If I jump out of the seat they all Anchor properly, and when I sit again they all become Unanchored and the vehicle will move properly.
If I jump off the seat the Vehicle is removed and then regens, but when I jump on the new vehicle that tread is still the only one Unanchored.
This script has worked well in other games (with slight modifications) without issues. I got the script from a free model that had it set up to Anchor/Unanchor vehicle Parts and I modified it so it does the same to my treads so that the Unanchored treads and all their Contacts don’t cause lag in my game.
It seems to be an issue with the scan of the table not Unanchoring the 1st item in the Tracks Model.
local light = script.Parent.Parent.Bodykit.WedgeLight.SurfaceLight
all,last = {},nil
h = nil
seat = script.Parent
seat.ChildAdded:Connect(function(child)
if child.Name == "SeatWeld" and child:IsA("Weld") then
local h = child.Part1.Parent.Humanoid
if seat.SeatWeld then --print("sitting")
wait(.1)
function scan(p) --print("scanning")
for _,v in pairs(p:GetChildren()) do
if (v:IsA("BasePart")) then
if (last) then
for _,v in pairs(all) do
v.Anchored = false
end
end
table.insert(all,v)
last = v
end
scan(v)
end
end
scan(script.Parent.Parent.Tracks)
seat.Anchored = false --print("Chassis unanchored")
light.Enabled = true
end
end
end)
seat.ChildRemoved:Connect(function(child)
if child.Name == "SeatWeld" and child:IsA("Weld") then
function scan(p)
for _,v in pairs(p:GetChildren()) do
if (v:IsA("BasePart")) then
if (last) then
for _,v in pairs(all) do
v.Anchored = true
end
end
table.insert(all,v)
last = v
end
scan(v)
end
end
scan(script.Parent.Parent.Tracks)
seat.Anchored = true --print("Chassis anchored")
light.Enabled = false
end
end)