Hello everyone!
I’m trying to create a moving road and moving scenery (to create the effect that the car is moving), but while scripting the syncing for the road and the scenery, I’ve run into some problems.
The first problem is that there is a slight difference in the time the road tween takes and the time the scenery tween takes. The difference is most noticeable when I change the speed of the vehicle, which is supposed to make the road and scenery pass by slower. I used a certain formula to calculate the tween time by distance and speed, so perhaps there might be an error in that.
The second problem (not really a problem, at least for the scripting) is that I suck at math, while this is just a case of math.
Here are the scripts for reference:
Road Tween Script (doesn’t have any issues, but is still important to show)
local TweenService = game:GetService("TweenService")
local ServerStorage = game:GetService("ServerStorage")
local EventResources = ServerStorage:WaitForChild("EventResources")
local ServerInfo = workspace:WaitForChild("ServerInfo")
local road = script.Parent.RoadModel
local hitboxes = road.Hitboxes
local roadRoot = road.PrimaryPart
local roadTouchedConnection
local grassTouchedConnection1
local grassTouchedConnection2
local moveTween
local function resetRoad()
local newTweenTime = ((roadRoot.Size.Z / 10) / (ServerInfo.VehicleSpeed.Value / 5))
local moveInfo = TweenInfo.new(newTweenTime, Enum.EasingStyle.Linear)
moveTween = TweenService:Create(roadRoot, moveInfo, {CFrame = script.Parent.End.CFrame})
moveTween:Play()
moveTween.Completed:Wait()
road:SetPrimaryPartCFrame(script.Parent.Start.CFrame)
end
while task.wait() do
resetRoad()
end
Scenery Tween Script
local TweenService = game:GetService("TweenService")
local ServerStorage = game:GetService("ServerStorage")
local RoadObjects = ServerStorage:WaitForChild("RoadObjects"):GetChildren()
local ServerInfo = workspace:WaitForChild("ServerInfo")
local road = script.Parent.RoadModel
local roadZSize = 2400
local objectsSpawned = 0
local lastSpawnedObject = nil
local moveTweens = {}
function getTimeByDistance(distance, speed)
return (distance / (5 / 4)) / (speed / 5)
end
local function newObject()
local newTweenTime = ((roadZSize / (5 / 4)) / (ServerInfo.VehicleSpeed.Value / 5))
local moveInfo = TweenInfo.new(newTweenTime, Enum.EasingStyle.Linear)
local newObject = RoadObjects[math.random(1, #RoadObjects)]:Clone()
objectsSpawned += 1
lastSpawnedObject = newObject
newObject.Name = newObject.Name .. objectsSpawned
newObject.Parent = road
moveTweens[newObject.Name] = TweenService:Create(newObject.PrimaryPart, moveInfo, {CFrame = script.Parent.End.CFrame})
moveTweens[newObject.Name]:Play()
repeat
task.wait(1)
until newObject.PrimaryPart.Position == script.Parent.End.Position
newObject:Destroy()
end
ServerInfo.VehicleSpeed.Changed:Connect(function(value)
for i, object in road:GetChildren() do
local lastCFrame = object.PrimaryPart.CFrame
moveTweens[object.Name]:Cancel()
object:PivotTo(lastCFrame)
local realObjectPositionZ
if object.PrimaryPart.Position.Z > 0 then
realObjectPositionZ = object.PrimaryPart.Position.Z
elseif object.PrimaryPart.Position.Z < 0 then
realObjectPositionZ = object.PrimaryPart.Position.Z * -1
end
local differenceRealAndZero
local differenceEndAndZero
local newMoveInfo = TweenInfo.new(getTimeByDistance(roadZSize - ((realObjectPositionZ + script.Parent.End.Position.Z) / script.Parent.End.Position.Z), ServerInfo.VehicleSpeed.Value), Enum.EasingStyle.Linear)
moveTweens[object.Name] = TweenService:Create(object.PrimaryPart, newMoveInfo, {CFrame = script.Parent.End.CFrame })
moveTweens[object.Name]:Play()
end
end)
task.wait(1)
task.spawn(function()
while true do
if ServerInfo.VehicleSpeed.Value > 0 then
task.spawn(newObject)
end
local lastSpawnedObjectRealPositionZ
if lastSpawnedObject then
if lastSpawnedObject.PrimaryPart.Position.Z > 0 then
lastSpawnedObjectRealPositionZ = lastSpawnedObject.PrimaryPart.Position.Z
elseif lastSpawnedObject.PrimaryPart.Position.Z < 0 then
lastSpawnedObjectRealPositionZ = lastSpawnedObject.PrimaryPart.Position.Z * -1
end
repeat
task.wait()
until (roadZSize - ((lastSpawnedObjectRealPositionZ + (script.Parent.Start.Position.Z * -1)) / (script.Parent.Start.Position.Z * -1))) > lastSpawnedObject.PrimaryPart.Size.Z
end
task.wait(10)
end
end)
Video showcasing the problem
During the video, you might notice the scenery passing by faster due to me adjusting the vehicle speed.
It’s hardly noticeable, but you can see it the most when the scenery has crossed the center of the map (where the camper van is)