Whats wrong with my cable for my cable car script?

So I am building a cable car and decided to use

script.Parent.Velocity = Vector3.new(5,0,0)

Why I am using this? It is because the cable is just in a straight line and does not require and turns. So I find the position of one of the parts of the Cable car to locate its position. I use the train floor since it is more in the center. The properties tab of the part says X is at 42, however the location is not the same (check the output I put at the bottom) The reason why I used

NumberRange.new()

Was because I thought it would cover all the numbers within that range
I have done this

while true do
	local Train = game.Workspace.Trains.CableCar.TrainFloor.Position
	print(Train.X)
	if Train == NumberRange.new(40,45) then
		print("Train at 42X")
		while true do
			script.Parent.Velocity = Vector3.new(5,0,0)
			print("Train moving towards Station2")
			if Train == NumberRange.new(60,65) then 
				break
			end
		end
	end
	if Train == NumberRange.new(60,65) then
		print("Train at 60X")
		script.Parent.Velocity = Vector3.new(-5,0,0)
		print("Train Moving back to Hub")
		if Train == NumberRange.new(40,45) then
			break
		end
	end
	wait(1)
end

However my output is
41.99592590332 (x56)

I cannot move the entire model to 42X and the floor stays at that position even as I try to change it. So, any help?

1 Like

The location not being exactly 42 is because of some calculation inaccuracy. If you are having problems moving the cable, maybe try unanchoring and welding all other parts to the floor and keeping the floor anchored and updating its CFrame every frame instead of setting the velocity.

Will it run smoothly like velocity? Cause I only use CFrame for teleportion. If not, must I keep on updating the CFrame like do X+0.5 for it to run smoothly?

Try if this works. I tested it with a single part and it succesfully moved it between two points. If you have done the unanchoring and welding, you don’t need the for loop.

local RunService = game:GetService("RunService")

local VELOCITY_X = 5 -- studs in second
local MIN_X, MAX_X = 42, 60

local floor = workspace.Trains.CableCar.TrainFloor
local delta = 0

for i, v in ipairs(cableCar:GetDescendants()) do
	if v ~= floor and v:IsA("Part") and not v:FindFirstChildOfClass("WeldConstraint") then
		v.Anchored = false
		local w = Instance.new("WeldConstraint")
		w.Part0 = v
		w.Part1 = floor
		w.Parent = v
	end
end

while true do
	print("Train at 42X")
	while floor.Position.X < MAX_X do
		floor.CFrame += Vector3.new(delta*VELOCITY_X, 0, 0)
		print("Train moving towards Station2", delta)
		delta = RunService.Heartbeat:Wait()
	end
	print("Train at 60X")
	while floor.Position.X > MIN_X do
		floor.CFrame -= Vector3.new(delta*VELOCITY_X, 0, 0)
		print("Train Moving back to Hub")
		delta = RunService.Heartbeat:Wait()
	end
	wait(1)
end
1 Like

I used the alignposition method and it is much more effective. Thanks to all who replied