I am working for a special game for my friend and I have already built my custom train, included some sliding doors, and when I wanted to make the train move once I entered inside I included this:
local Train = script.Parent local function onTouch() for i = 0,1,.001 do Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0,i)) wait() end end script.Parent.Touched:connect(onTouch)
I placed this script in a part inside said Model(Train). The part is located directly in the middle of the train, so when I pass the sliding doors I immediately touch said part to make the train move, yet the train doesn't move when I touch it. I also placed a part above the train, naming it MainPart and making said MainPart into a primary part for my train. Can someone help me in on this? I am still a beginner at scripting.
local Train = script.Parent local plrs = game:GetService('Players') local function runtrain() for i = 0,1,.001 do Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0,i)) wait() end end for _, i in pairs(Train:GetDescendants()) do if i:IsA('BasePart') then i.Touched:connect(function (hit) if plrs:FindFirstChild(hit.Parent.Name) then runtrain() end end) end end
If that didn't work try this
local Train = script.Parent local plrs = game:GetService('Players') local function runtrain() for i = 0,1,.001 do Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0,i)) wait() end end while true do wait() for _, i in pairs(Train:GetDescendants()) do if i:IsA('BasePart') then for _, hit in pairs(i:GetTouchingParts()) do if plrs:FindFirstChild(hit.Parent.Name) then runtrain() end end end wait() end end
One of these should of worked