City Traffic System [RESOLVED]

Hello! I am trying to achieve a simple yet smooth city traffic system for my game that is taking place in, well, a city. I looked around devforum and I read a few and I them but I don’t think I quite understood it fully so I was wondering if I did something wrong or what since I am not that knowledgeable with CFrame and Tweening.

Code:
Server Script

local speed = script.Parent.Body.Speed
local carBody = script.Parent.Body
local newCarPosition
local drivingEvent = script.Parent.driving

while wait(1) do
	print("moving")
	newCarPosition = carBody.CFrame == carBody.CFrame + Vector3.new(0,1,0)
	drivingEvent:FireAllClients(newCarPosition, speed, carBody)
end

Local Script

local drivingEvent = script.Parent.driving
local tweenService = game:GetService("TweenService")

drivingEvent.OnClientEvent:Connect(function(position, speed, car)
	local tweenInfo = TweenInfo.new(
		
		speed,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		1,
		false,
		0
	)
	local carProperties = {
		Position = position
	}
	local Tween = tweenService:Create(car, tweenInfo, carProperties)
	Tween:Play()
end)

You’re using == while trying to set the newCarPosition.
I would recommend using task.wait() instead of wait.

local speed = script.Parent.Body.Speed
local carBody = script.Parent.Body
local newCarPosition
local drivingEvent = script.Parent.driving

while true do
    print("moving")
    newCarPosition = carBody.Position + Vector3.new(0, 1, 0)
    drivingEvent:FireAllClients(newCarPosition, speed, carBody)
    task.wait(1) --consider changing to just task.wait()
end

local drivingEvent = script.Parent.driving
local tweenService = game:GetService("TweenService")

drivingEvent.OnClientEvent:Connect(function(position, speed, car)
    local tweenInfo = TweenInfo.new(
        speed,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.Out,
        0, -- 0 will make this run infinitely but should update the cars position when the server sends a new request (might want to play around with it though) 
        false,
        0
    )
    local carProperties = {
        Position = UDim2.new(0, position.X, 0, position.Y)
    }
    local Tween = tweenService:Create(car, tweenInfo, carProperties)
    Tween:Play()
end)
1 Like

Oh shoot! I don’t even know how I didn’t notice I used == instead of +, thank you for the help I will try these out.

I have been trying this out and it has been working better than the last one and is actually registering, but I am getting this error:

local carProperties = {
        Position = UDim2.new(0, position.X, 0, position.Y)
    }

to

local carProperties = {
        Position = Vector3.new(0, position.X, 0, position.Y) 
    }

It should be Vector3.new instead of Udim2.new

Also, you have 4 positional values. I think Vector3.new only accepts 3 values.

Ah. thank you. I adjusted the position values to three and did the adjustment and now it works.

1 Like

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