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)