Controllable train script does not work

Hello. I was attempting to make a train where players can control it. When I scripted the functions, it does not work.
Script: (its very long ik)

local set = script.Parent
local value = set.speedvalue
local go = set.go
local speed = set.speed
local speed2 = set.speed2
local add = set.add
local sub = set.subtract
local stop = set.stop
local move1 = set.movable1
local move2 = set.movable2

local door1 = set.Parent.Door1
local door2 = set.Parent.door2

local cooldowncolor = Color3.new(0.258824, 0.258824, 0.258824)
local stopcolor = stop.BrickColor
local gocolor = go.BrickColor
local color = add.Color

local clickdistance = 20

value.Value = 15
wait(0.01)
speed2.SurfaceGui.TextLabel.Text = value.Value

add.ClickDetector.MouseClick:Connect(function()
	add.Color = cooldowncolor
	value.Value = value.Value + 5
	add.ClickDetector.MaxActivationDistance = 0
	if (speed.Value > 200) then speed.Value = 200 end
	wait(0.01)
	speed2.SurfaceGui.TextLabel.Text = value.Value
	wait(0.2)
	add.ClickDetector.MaxActivationDistance = clickdistance
	add.Color = color
end) 

sub.ClickDetector.MouseClick:Connect(function()
	sub.Color = cooldowncolor
	value.Value = value.Value - 5
	sub.ClickDetector.MaxActivationDistance = 0
	if (speed.Value < 200) then speed.Value = 0 end
	wait(0.01)
	speed2.SurfaceGui.TextLabel.Text = value.Value
	wait(0.2)
	sub.ClickDetector.MaxActivationDistance = clickdistance
	sub.Color = color
end) 

go.ClickDetector.MouseClick:Connect(function()
	move1.Velocity = script.Parent.CFrame.lookVector *value.Value
	for _, doorparts in pairs(door2:GetDescendants()) do
		if doorparts.Name == "window" then
			doorparts.Transparency = 0.5
			doorparts.CanCollide = true
		elseif doorparts.Name == "door" then
			doorparts.Transparency = 0
			doorparts.CanCollide = true
		end
	end
	for _, doorparts in pairs(door1:GetDescendants()) do
		doorparts.Transparency = 1
		doorparts.CanCollide = false
	end
end)

stop.ClickDetector.MouseClick:Connect(function()
	move1.Velocity = script.Parent.CFrame.lookVector *0
	for _, doorparts in pairs(door1:GetDescendants()) do
		if doorparts.Name == "window" then
			doorparts.Transparency = 0.5
			doorparts.CanCollide = true
		elseif doorparts.Name == "door" then
			doorparts.Transparency = 0
			doorparts.CanCollide = true
		end
	end
	for _, doorparts in pairs(door2:GetDescendants()) do
		doorparts.Transparency = 1
		doorparts.CanCollide = false
	end
end)

here is the items in properties

Which function doesn’t work?, and don’t name the variable sub cause that is a built in keyword

I think it’s the CFrame.lookVector part that isn’t working.

Why are you doing * 0?, that makes move.Velocity = 0

So the train could be stopped upon clicking a button.

I don’t think you can times any Cframe by 0 try this

move1.Velocity = script.Parent.CFrame.lookVector * CFrame.new(0,0,0)

It doesn’t work still.

oh yea i improved the script a lil

local set = script.Parent
local value = set.speedvalue
local go = set.go
local speed = set.speed
local speed2 = set.speed2
local add = set.add
local subt = set.subtract
local stop = set.stop
local move1 = set.movable1
local move2 = set.movable2

local door1 = set.Parent.Door1
local door2 = set.Parent.door2

local cooldowncolor = Color3.new(0.258824, 0.258824, 0.258824)
local stopcolor = stop.BrickColor
local gocolor = go.BrickColor
local color = add.Color

local clickdistance = 20

move1.Velocity = move1.CFrame.lookVector * CFrame.new(0,0,0)
move2.Velocity = move2.CFrame.lookVector * CFrame.new(0,0,0)


value.Value = 15
wait(0.01)
speed2.SurfaceGui.TextLabel.Text = value.Value

add.ClickDetector.MouseClick:Connect(function()
	add.Color = cooldowncolor
	value.Value = value.Value + 5
	add.ClickDetector.MaxActivationDistance = 0
	if (speed.Value > 200) then speed.Value = 200 end
	wait(0.01)
	speed2.SurfaceGui.TextLabel.Text = value.Value
	wait(0.2)
	add.ClickDetector.MaxActivationDistance = clickdistance
	add.Color = color
end) 

subt.ClickDetector.MouseClick:Connect(function()
	subt.Color = cooldowncolor
	value.Value = value.Value - 5
	subt.ClickDetector.MaxActivationDistance = 0
	if (speed.Value < 200) then speed.Value = 0 end
	wait(0.01)
	speed2.SurfaceGui.TextLabel.Text = value.Value
	wait(0.2)
	subt.ClickDetector.MaxActivationDistance = clickdistance
	subt.Color = color
end) 

go.ClickDetector.MouseClick:Connect(function()
	while true do
		move1.Velocity = move1.CFrame.lookVector *value.Value
		move2.Velocity = move2.CFrame.LookVector *value.Value
		wait(0.1)
	end
	for _, doorparts in pairs(door2:GetDescendants()) do
		if doorparts.Name == "window" then
			doorparts.Transparency = 0.5
			doorparts.CanCollide = true
		elseif doorparts.Name == "door" then
			doorparts.Transparency = 0
			doorparts.CanCollide = true
		end
	end
	for _, doorparts in pairs(door1:GetDescendants()) do
		doorparts.Transparency = 1
		doorparts.CanCollide = false
	end
end)

stop.ClickDetector.MouseClick:Connect(function()
	while true do
		move1.Velocity = move1.CFrame.lookVector * CFrame.new(0,0,0)
		move2.Velocity = move2.CFrame.lookVector * CFrame.new(0,0,0)
		wait(0.1)
	end
	for _, doorparts in pairs(door1:GetDescendants()) do
		if doorparts.Name == "window" then
			doorparts.Transparency = 0.5
			doorparts.CanCollide = true
		elseif doorparts.Name == "door" then
			doorparts.Transparency = 0
			doorparts.CanCollide = true
		end
	end
	for _, doorparts in pairs(door2:GetDescendants()) do
		doorparts.Transparency = 1
		doorparts.CanCollide = false
	end
end)

the output


EDIT: ok nvm the cframe.new part is supposed to be vector3.new
ANOTHER EDIT: doorparts part wont work

have you tried to set the AssemblyLinearVelocity and or the ApplyImpulse

you will need to do this multiple times to make it move as it constantly loses velocity
also make sure all parts are unanchored

on this loop right here it never stops so the code below it for doors will never run

2 Likes

Oh yeah that’s probably it, it can’t stop if it’s moving forever

Create a velocity variable that can be set by any of your functions at the top of the code. I just added it here since this looks like some speed stuff.

**local velocity = 0** -- The Train's Speed is 0
value.Value = 15
wait(0.01)
speed2.SurfaceGui.TextLabel.Text = value.Value

In your go and stop code, instead of your while true do loops, just set a velocity variable. Below your last function add the while loop shown.

go.ClickDetector.MouseClick:Connect(function()
	
    velocity = 15 -- Sets velocity to 15 studs per sec

	for _, doorparts in pairs(door2:GetDescendants()) do
		if doorparts.Name == "window" then
			doorparts.Transparency = 0.5
			doorparts.CanCollide = true
		elseif doorparts.Name == "door" then
			doorparts.Transparency = 0
			doorparts.CanCollide = true
		end
	end
	for _, doorparts in pairs(door1:GetDescendants()) do
		doorparts.Transparency = 1
		doorparts.CanCollide = false
	end
end)

stop.ClickDetector.MouseClick:Connect(function()
	
    velocity = 0 -- Sets velocity to 0

	for _, doorparts in pairs(door1:GetDescendants()) do
		if doorparts.Name == "window" then
			doorparts.Transparency = 0.5
			doorparts.CanCollide = true
		elseif doorparts.Name == "door" then
			doorparts.Transparency = 0
			doorparts.CanCollide = true
		end
	end
	for _, doorparts in pairs(door2:GetDescendants()) do
		doorparts.Transparency = 1
		doorparts.CanCollide = false
	end
end)


-- Speed loop
while task.wait(0.1) do
   move1.AssemblyLinearVelocity = move1.CFrame.LookVector * velocity
   move2.AssemblyLinearVelocity = move2.CFrame.LookVector * velocity
end

Theoretically works, but I’m too lazy to test, but Nyonic was right about AssembleLinearVelocity; plus you can multiply LookVectors but a single digit rather than another CFrame or Vector3.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.