Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

5 comments

0

Still didn't make my train move.

0

Were the trains never moving to begin with? I normally weld my stuff and use a body velocity for the movement

0

I didn't weld anything, I just anchored the entire train after I finished building it

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.

0

Alright, I'll try and weld the parts and then use body velocity for the movement.

This discussion is preserved as it was. New answers, comments, and votes are closed.