What should i replace while task.wait do() with?

local collectionService = game:GetService("CollectionService")

local function boatStuff(boat)
    local driversSeat = boat.VehicleSeat
    local raycastParams = RaycastParams.new()

    raycastParams.FilterDescendantsInstances = {workspace.Terrain}
    raycastParams.FilterType = Enum.RaycastFilterType.Whitelist

    driversSeat.Changed:Connect(function()
        boat.AngularVelocity.AngularVelocity = Vector3.new(0, -1 * driversSeat.Steer, 0)
        boat.LinearVelocity.LineVelocity = 50 * driversSeat.Throttle
    end)

    while task.wait() do
        local startingPos = boat.VehicleSeat.Position + Vector3.new(0, 5, 0)
        local endingPos = boat.VehicleSeat.Position - Vector3.new(0, 15, 0)
        local ray = workspace:Raycast(startingPos, endingPos - startingPos, raycastParams)

        if ray and ray.Material == Enum.Material.Water then
            boat.AngularVelocity.Enabled = true
            boat.LinearVelocity.Enabled = true
        else
            boat.AngularVelocity.Enabled = false
            boat.LinearVelocity.Enabled = false
        end
    end
end

for _, boat in pairs(collectionService:GetTagged("Boat")) do
    print(boat.Name)
    boatStuff(boat)
end

at the moment this is only printing one of the models tagged with the Boat tag, how can I change my code so that it will take in all models that are tagged with the boat tag

what can i use or do instead of the while wait loop which is causing the issue while still making sure that the boat will not move if it is not touching the water terrain.

many thanks in advance :smiley:

U should Run it before every physics frame.
RunService.Heartbeat

thank you that worked much appreciated :slight_smile:

That would be Stepped, not Heartbeat as Heartbeat fires after every physics frame

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