I am currently scripting the script that welds to the part when the anchor is off for the train system. It worked and welded but there is error.
The script when anchor is off
local Players = game:GetService("Players")
local cars1 = script.Parent
local cars2 = script.Parent.Parent.Parent.Carriage2.WeldingHumanoids
local cars3 = script.Parent.Parent.Parent.Carriage3.WeldingHumanoids
local cars4 = script.Parent.Parent.Parent.Carriage4.WeldingHumanoids
local bools = true
local function touchontrain1 (hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
local humanoid = hit.Parent:findFirstChild("Humanoid")
local humanoid2 = hit.Parent:findFirstChild("HumanoidRootPart")
local name = player.Name
if player.Team.Name ~= "Driver/運転士" and name and humanoid ~= nil then
humanoid.JumpPower = 0
humanoid.WalkSpeed = 0
wait(2)
humanoid.PlatformStand = true
local weld = Instance.new("WeldConstraint")
player.Character.HumanoidRootPart.Massless = true
player.Character.LeftFoot.Massless = true
player.Character.LeftHand.Massless = true
player.Character.LeftLowerArm.Massless = true
player.Character.LeftLowerLeg.Massless = true
player.Character.LeftUpperArm.Massless = true
player.Character.LeftUpperLeg.Massless = true
player.Character.LowerTorso.Massless = true
player.Character.RightFoot.Massless = true
player.Character.RightHand.Massless = true
player.Character.RightLowerArm.Massless = true
player.Character.RightLowerLeg.Massless = true
player.Character.RightUpperArm.Massless = true
player.Character.RightUpperLeg.Massless = true
player.Character.UpperTorso.Massless = true
player.Character.Head.Massless = true
weld.Part0 = humanoid2
weld.Part1 = cars1
weld.Name = name
if weld and name then
weld.Parent = cars1
end
end
end
local function touchontrain2 (hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
local humanoid = hit.Parent:findFirstChild("Humanoid")
local humanoid2 = hit.Parent:findFirstChild("HumanoidRootPart")
local name = player.Name
if player.Team.Name ~= "Driver/運転士" and name and humanoid ~= nil then
humanoid.JumpPower = 0
humanoid.WalkSpeed = 0
wait(1.5)
humanoid.PlatformStand = true
local weld = Instance.new("WeldConstraint")
player.Character.HumanoidRootPart.Massless = true
player.Character.LeftFoot.Massless = true
player.Character.LeftHand.Massless = true
player.Character.LeftLowerArm.Massless = true
player.Character.LeftLowerLeg.Massless = true
player.Character.LeftUpperArm.Massless = true
player.Character.LeftUpperLeg.Massless = true
player.Character.LowerTorso.Massless = true
player.Character.RightFoot.Massless = true
player.Character.RightHand.Massless = true
player.Character.RightLowerArm.Massless = true
player.Character.RightLowerLeg.Massless = true
player.Character.RightUpperArm.Massless = true
player.Character.RightUpperLeg.Massless = true
player.Character.UpperTorso.Massless = true
player.Character.Head.Massless = true
weld.Part0 = humanoid2
weld.Part1 = cars2
weld.Name = name
if weld and name then
weld.Parent = cars2
end
end
end
local function touchontrain3 (hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
local humanoid = hit.Parent:findFirstChild("Humanoid")
local humanoid2 = hit.Parent:findFirstChild("HumanoidRootPart")
local name = player.Name
if player.Team.Name ~= "Driver/運転士" and name and humanoid ~= nil then
humanoid.JumpPower = 0
humanoid.WalkSpeed = 0
wait(1.5)
humanoid.PlatformStand = true
local weld = Instance.new("WeldConstraint")
player.Character.HumanoidRootPart.Massless = true
player.Character.LeftFoot.Massless = true
player.Character.LeftHand.Massless = true
player.Character.LeftLowerArm.Massless = true
player.Character.LeftLowerLeg.Massless = true
player.Character.LeftUpperArm.Massless = true
player.Character.LeftUpperLeg.Massless = true
player.Character.LowerTorso.Massless = true
player.Character.RightFoot.Massless = true
player.Character.RightHand.Massless = true
player.Character.RightLowerArm.Massless = true
player.Character.RightLowerLeg.Massless = true
player.Character.RightUpperArm.Massless = true
player.Character.RightUpperLeg.Massless = true
player.Character.UpperTorso.Massless = true
player.Character.Head.Massless = true
weld.Part0 = humanoid2
weld.Part1 = cars3
weld.Name = name
if weld and name then
weld.Parent = cars3
end
end
end
local function touchontrain4 (hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
local humanoid = hit.Parent:findFirstChild("Humanoid")
local humanoid2 = hit.Parent:findFirstChild("HumanoidRootPart")
local name = player.Name
if player.Team.Name ~= "Driver/運転士" and name and humanoid ~= nil then
humanoid.JumpPower = 0
humanoid.WalkSpeed = 0
wait(1.5)
humanoid.PlatformStand = true
local weld = Instance.new("WeldConstraint")
player.Character.HumanoidRootPart.Massless = true
player.Character.LeftFoot.Massless = true
player.Character.LeftHand.Massless = true
player.Character.LeftLowerArm.Massless = true
player.Character.LeftLowerLeg.Massless = true
player.Character.LeftUpperArm.Massless = true
player.Character.LeftUpperLeg.Massless = true
player.Character.LowerTorso.Massless = true
player.Character.RightFoot.Massless = true
player.Character.RightHand.Massless = true
player.Character.RightLowerArm.Massless = true
player.Character.RightLowerLeg.Massless = true
player.Character.RightUpperArm.Massless = true
player.Character.RightUpperLeg.Massless = true
player.Character.UpperTorso.Massless = true
player.Character.Head.Massless = true
weld.Part0 = humanoid2
weld.Part1 = cars4
weld.Name = name
if weld and name then
weld.Parent = cars4
end
end
end
cars1.Touched:Connect(touchontrain1)
cars2.Touched:Connect(touchontrain2)
cars3.Touched:Connect(touchontrain3)
cars4.Touched:Connect(touchontrain4)
The script when the anchor is on
local Players = game:GetService("Players")
function onTouch(part)
local humanoid = part.Parent:findFirstChild("Humanoid")
local player = Players:GetPlayerFromCharacter(part.Parent)
if humanoid ~= nil then
humanoid.JumpPower = 10
humanoid.WalkSpeed = 16
humanoid.PlatformStand = false
player.Character.HumanoidRootPart.Massless = false
player.Character.LeftFoot.Massless = false
player.Character.LeftHand.Massless = false
player.Character.LeftLowerArm.Massless = false
player.Character.LeftLowerLeg.Massless = false
player.Character.LeftUpperArm.Massless = false
player.Character.LeftUpperLeg.Massless = false
player.Character.LowerTorso.Massless = false
player.Character.RightFoot.Massless = false
player.Character.RightHand.Massless = false
player.Character.RightLowerArm.Massless = false
player.Character.RightLowerLeg.Massless = false
player.Character.RightUpperArm.Massless = false
player.Character.RightUpperLeg.Massless = false
player.Character.UpperTorso.Massless = false
player.Character.Head.Massless = false
end
end
local cars1 = script.Parent
local cars2 = script.Parent.Parent.Parent.Carriage2.WeldingHumanoids
local cars3 = script.Parent.Parent.Parent.Carriage3.WeldingHumanoids
local cars4 = script.Parent.Parent.Parent.Carriage4.WeldingHumanoids
cars1.Touched:connect(onTouch)
cars2.Touched:connect(onTouch)
cars3.Touched:connect(onTouch)
cars4.Touched:connect(onTouch)
The script for control
local changement = script.Parent.Parent.Parent.State.Anchors
changement.Changed:Connect(function()
if changement.Value == false then
script.Parent.WeldBreak.Enabled = false
script.Parent.Scriptweld.Enabled= true
elseif changement.Value == true then
script.Parent.WeldBreak.Enabled = true
script.Parent.Scriptweld.Enabled = false
for _ , descendant in ipairs(script.Parent.Parent.WeldingHumanoids:GetDescendants()) do
if descendant:IsA("WeldConstraint") then
local Weld = descendant
Weld:Destroy()
end
end
for _ , descendant in ipairs(script.Parent.Parent.Parent.Carriage2.WeldingHumanoids:GetDescendants()) do
if descendant:IsA("WeldConstraint") then
local Weld = descendant
Weld:Destroy()
end
end
for _ , descendant in ipairs(script.Parent.Parent.Parent.Carriage3.WeldingHumanoids:GetDescendants()) do
if descendant:IsA("WeldConstraint") then
local Weld = descendant
Weld:Destroy()
end
end
for _ , descendant in ipairs(script.Parent.Parent.Parent.Carriage4.WeldingHumanoids:GetDescendants()) do
if descendant:IsA("WeldConstraint") then
local Weld = descendant
Weld:Destroy()
end
end
end
end)
If possible, please explain me why.