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

How to make a train move once I have entered inside said train?

Asked by 6 years ago

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.

1 answer

Log in to vote
0
Answered by
seith14 206 Moderation Voter
6 years ago
Edited 6 years ago
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

0
Still didn't make my train move. coolboy90082 -1 — 6y
0
Were the trains never moving to begin with? I normally weld my stuff and use a body velocity for the movement seith14 206 — 6y
0
I didn't weld anything, I just anchored the entire train after I finished building it coolboy90082 -1 — 6y
0
And yes, the train was never moving to begin with. It's just there. I just wish for the player to get transported to another spot once they hop inside. coolboy90082 -1 — 6y
0
Alright, I'll try and weld the parts and then use body velocity for the movement. coolboy90082 -1 — 6y
Ad

Answer this question