Moving Vehicle [Dead Rails]

  1. What do you want to achieve? I want to make a vehicle that can transport players and objects using roblox physics just like Dead Rails does.

  2. What is the issue? I simply can’t make it. My vehicle is a model with a bunch of parts welded to a anchored primary part that i move using TweenService.

  3. What solutions have you tried so far? I have looked at posts concerning this system, i have looked at Roblox Documentation for every instance concerning roblox physics. I have succeeded in some sort by combining AssemblyLinearVelocity and Tween but its very glitchy.

Here is my current script:

local tweenService = game:GetService("TweenService")

local van = script.Parent
local lever = van.Lever

local center = lever.Center
local primary = van.PrimaryPart

local switchOn = lever.On
local switchOff = lever.Off

local moveThread = nil
local moveTween = nil
local moveSpeed = 40
local targetPosition = primary.Position

for _, descendant in pairs(van:GetDescendants()) do
	if descendant:IsA("BasePart") and descendant ~= primary then
		local weldConstraint = Instance.new("WeldConstraint")
		weldConstraint.Part0 = descendant
		weldConstraint.Part1 = primary
		weldConstraint.Parent = descendant
	end
end

local function switchVisibility(switch : BasePart, visible)
	if visible then
		switch.Transparency = 0
		switch.CanCollide = true
		switch.CanQuery = true
		switch.CanTouch = true
	else
		switch.Transparency = 1
		switch.CanCollide = false
		switch.CanQuery = false
		switch.CanTouch = false
	end
end

switchOn.ClickDetector.MouseClick:Connect(function()
	switchOn.ClickDetector.MaxActivationDistance = 0
	switchVisibility(switchOn, false)
	center.ToggleSound:Play()

	moveThread = task.spawn(function()
		while true do
			primary.AssemblyLinearVelocity = primary.CFrame.LookVector * moveSpeed
			moveTween = tweenService:Create(primary, TweenInfo.new(1, Enum.EasingStyle.Linear), {CFrame = primary.CFrame * CFrame.new(0, 0, -moveSpeed)})
			moveTween:Play()
			moveTween.Completed:Wait()
		end
	end)
	primary["Car Engine"]:Play()

	switchVisibility(switchOff, true)
	switchOff.ClickDetector.MaxActivationDistance = 10
end)

switchOff.ClickDetector.MouseClick:Connect(function()
	switchOff.ClickDetector.MaxActivationDistance = 0
	switchVisibility(switchOff, false)
	center.ToggleSound:Play()

	if moveThread then
		task.cancel(moveThread)
		moveThread = nil
	end
	if moveTween then
		moveTween:Pause()
		moveTween = nil
	end
	primary["Car Engine"]:Stop()

	primary.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
	switchVisibility(switchOn, true)
	switchOn.ClickDetector.MaxActivationDistance = 10
end)

First of all, wrong category, this should go on #help-and-feedback:scripting-support

What do you mean by Glitchy? Does the model rubber band, stand still for a short moment or something like that

Tweens also just work kinda oddly when played on the server, the server gives players ownership of parts to do psychics, which doesn’t help either

Probably keeping the movement logic on a RunService event instead of spawning every time it starts up

Yeah change the category so someone that can help actually encounters the thread

Thank you for pointing the category.

When i meant glitchy, it’s that when the van is moving, it goes back and forward. The van still moves forward, i think that this is a visual bug.

Also how do you recommend to use RunService, if possible can you provide a script or further details.

local moving=false

game:GetService("RunService").Heartbeat:Connect(function()

if moving then

if  not primary["Car Engine"].IsPlaying then
 primary["Car Engine"]:Play()
 --Also do the same for when moving is false and the sound is playing 
end
--movement logic
end

end)

With the tween system you have going on right now you have to keep track if there’s a tween playing so it doesn’t try to play two tweens at once

I hate not being able to index here

In order to transport players and items, you need to move the vehicle via physics. For dead rails we use a system that uses prismatic constraints to move the train.

TweenService is not considered physics-based movement so it will just slide out from underneath the players feet. Also, tweening on the server will look very laggy since it has to send a lot of data to the clients for replication.

4 Likes

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