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

A simple script. How do I make a train move?

Hello ROBLOXians,

I am currently trying to work on a game that for so long I haven't been able to figure out a script for. My knowledge of scripting is extremely basic - however, if anybody could help me on this I'd be extremely grateful.

So, my idea is: I have a train, and I want it to move simply from one side to another, and at a relatively fast speed. Not just that, when the train hits a wall, it bounces back and starts going the other way, hits the wall and again bounces back. It needs to be an infinite loop! If possible I'd need the script to just slot right into my train as A MODEL and work!

Finally, I believe that I, MYSELF, could do this, however I need the train to kill all players when they touch any part of the it.

Again, if anyone could help me I'd be very happy, MAYBE, willing to pay you some bucks on another account of mine.

-Crossy

1 comment

2

This website is for people that have questions, not requests. Check out the roblox studio wiki for what you need.

1 answer

If the train is not animated, you can simply move its parts to the next location you want it using SetPrimaryPartCFrame (make sure all the parts of the train are Anchored). You'll want to get rotation in there eventually, but a starting loop:

local train = workspace.Train
local minPos = -50
local maxPos = 50
local speed = 10
local pos = minPos
while true do
	train:SetPrimaryPartCFrame(CFrame.new(Vector3.new(pos, 5, 0)))
	pos = pos + speed * wait() -- wait returns time passed
	if (speed > 0 and pos > maxPos) or (speed < 0 and pos < maxPos) then -- turn train around
		speed = -speed
	end
end

This loop doesn't have acceleration or rotation, but perhaps it's enough to get you started.

As for every part of it being deadly, a recursive loop can set that up:

function kill(part)
	-- try to kill the player who owns 'part' (ex get PlayerFromCharacter of part.Parent and part.Parent.Parent, or just try to find a humanoid in either of those places)
end
function ApplyRecursively(where, func) -- apply 'func' to children of 'where' recursively
	local ch = where:GetChildren()
	for i = 1, #ch do
		func(ch[i])
		ApplyRecursively(ch[i], func)
	end
end
ApplyRecursively(train, function(p)
	if p:IsA("BasePart") then
		p.Touched:Connect(kill)
	end
end

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