So I want to make a boat turn and if it is moving then it will turn and move in that direction.
But, whenever it turns any direction, the boat starts turning back forward.
Here is my script:
local dss = game:GetService("DataStoreService")
local leaderstatsStore = dss:GetDataStore("LeaderstatsData1")
local boatsStore = dss:GetDataStore("BoatsData1")
local TS = game:GetService("TweenService")
local TInfo = TweenInfo.new(10,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
local event = game.ReplicatedStorage.Moving
local turnEvent = game.ReplicatedStorage.TurnAction
local newBoatEvent = game.ReplicatedStorage.NewBoat
local submergeEvent = game.ReplicatedStorage.Submerge
local players_moving = {}
local boat_tweens = {}
local savedBoats = game.ServerStorage.SavedBoats
local function findBoat(plr)
for i, boat in pairs(workspace.Boats:GetChildren()) do
if boat.Owner.Value == plr.Name then
return boat
end
end
return nil
end
game.Players.PlayerAdded:Connect(function(plr)
players_moving[plr.Name] = false
local boat = nil
boat = boatsStore:GetAsync(plr.UserId)
if boat ~= nil then
local newBoat = savedBoats:WaitForChild(boat):Clone()
newBoat.Parent = workspace.Boats
newBoat.Owner.Value = plr.Name
local randomDock = workspace.Docks:GetChildren()[math.random(1,#workspace.Docks:GetChildren())]
if randomDock.Name == "X" then
newBoat:SetPrimaryPartCFrame(CFrame.new(randomDock.CFrame.Position.X + 43, newBoat.PrimaryPart.CFrame.Position.Y, randomDock.CFrame.Position.Z) * CFrame.Angles(0, math.rad(90), 0))
elseif randomDock.Name == "Z" then
newBoat:SetPrimaryPartCFrame(CFrame.new(randomDock.CFrame.Position.X, newBoat.PrimaryPart.CFrame.Position.Y, randomDock.CFrame.Position.Z + 43))
end
wait(1)
plr.Character.HumanoidRootPart.CFrame = CFrame.new(newBoat.PrimaryPart.CFrame.Position.X,newBoat.PrimaryPart.CFrame.Position.Y + 10, newBoat.PrimaryPart.CFrame.Position.Z)
boat_tweens[newBoat.Name] = TS:Create(newBoat.PrimaryPart, TInfo, {
CFrame = CFrame.new(newBoat.PrimaryPart.CFrame.Position + Vector3.new(200,0,0))
})
print("Success")
else
--boat is nil, new player
--we will give them a starter boat
local newBoat = savedBoats["Small Boat"]:Clone()
newBoat.Parent = workspace.Boats
newBoat.Owner.Value = plr.Name
local randomDock = workspace.Docks:GetChildren()[math.random(1,#workspace.Docks:GetChildren())]
if randomDock.Name == "X" then
newBoat:SetPrimaryPartCFrame(CFrame.new(randomDock.CFrame.Position.X + 43, newBoat.PrimaryPart.CFrame.Position.Y, randomDock.CFrame.Position.Z) * CFrame.Angles(0, math.rad(90), 0))
elseif randomDock.Name == "Z" then
newBoat:SetPrimaryPartCFrame(CFrame.new(randomDock.CFrame.Position.X, newBoat.PrimaryPart.CFrame.Position.Y, randomDock.CFrame.Position.Z + 43))
end
wait(1)
plr.Character.HumanoidRootPart.CFrame = CFrame.new(newBoat.PrimaryPart.CFrame.Position.X,newBoat.PrimaryPart.CFrame.Position.Y + 10, newBoat.PrimaryPart.CFrame.Position.Z)
boat_tweens[newBoat.Name] = TS:Create(newBoat.PrimaryPart, TInfo, {
CFrame = CFrame.new(newBoat.PrimaryPart.CFrame.Position + Vector3.new(200,0,0))
})
print("Success")
end
local leader = Instance.new("Folder",plr)
leader.Name = "leaderstats"
local cash = Instance.new("NumberValue",leader)
cash.Name = "Cash"
local statsData = nil
statsData = leaderstatsStore:GetAsync(plr.UserId)
if statsData ~= nil then
cash.Value = statsData
else
cash.Value = 100
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
for i, boat in pairs(workspace.Boats:GetChildren()) do
if boat.Owner.Value == plr.Name then
local success, errmess = pcall(function()
boatsStore:SetAsync(plr.UserId, boat.Name)
end)
if success then
print("Data saved successfully")
else
warn(errmess)
end
end
end
local success, errmess = pcall(function()
leaderstatsStore:SetAsync(plr.UserId, plr:WaitForChild("leaderstats").Cash.Value)
end)
if success then
print("Data saved successfully")
else
warn(errmess)
end
end)
event.OnServerEvent:Connect(function(plr)
local boat = findBoat(plr)
if not players_moving[plr.Name] then
players_moving[plr.Name] = true
if boat_tweens[boat.Name] == nil then
boat_tweens[boat.Name] = TS:Create(boat.PrimaryPart, TInfo, {
CFrame = CFrame.new(boat.PrimaryPart.CFrame.Position + Vector3.new(200,0,0)) * CFrame.Angles(boat.PrimaryPart.CFrame.lookVector)
})
end
while players_moving[plr.Name] do
print("Tween start")
boat_tweens[boat.Name]:Play()
boat_tweens[boat.Name].Completed:Wait()
print("Tween end")
boat_tweens[boat.Name] = TS:Create(boat.PrimaryPart, TInfo, {
CFrame = CFrame.new(boat.PrimaryPart.CFrame.Position + Vector3.new(200,0,0)) * CFrame.Angles(boat.PrimaryPart.CFrame.lookVector)
})
end
elseif players_moving[plr.Name] then
players_moving[plr.Name] = false
boat_tweens[boat.Name]:Cancel()
end
end)
turnEvent.OnServerEvent:Connect(function(plr, action)
local boat = findBoat(plr)
if action == "Left" then
boat.PrimaryPart.CFrame = boat.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(90), 0)
elseif action == "Right" then
boat.PrimaryPart.CFrame = boat.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(-90), 0)
elseif action == "Backward" then
boat.PrimaryPart.CFrame = boat.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(180), 0)
else
print(action.." is not left, right, forward or baackward")
end
end)
newBoatEvent.OnServerEvent:Connect(function(plr, boatName, price)
local stat = plr:WaitForChild("leaderstats").Cash
if stat.Value >= price then
local boat = findBoat(plr)
boat_tweens[boat.Name] = nil
boat:Destroy()
local newBoat = savedBoats:WaitForChild(boatName):Clone()
newBoat.Parent = workspace.Boats
newBoat.Owner.Value = plr.Name
boat_tweens[newBoat.Name] = TS:Create(newBoat.PrimaryPart, TInfo, {
CFrame = CFrame.new(newBoat.PrimaryPart.CFrame.Position + Vector3.new(200,0,0))
})
stat.Value = stat.Value - price
else
warn("player "..plr.Name.." does not have enough cash")
end
end)
submergeEvent.OnServerEvent:Connect(function(plr, submerge)
local boat = findBoat(plr)
if boat.Name == "Submarine" then
if not submerge then
boat:SetPrimaryPartCFrame(CFrame.new(boat.PrimaryPart.CFrame.Position.X, 4.5, boat.PrimaryPart.CFrame.Position.Z))
plr.Character.HumanoidRootPart.CFrame = CFrame.new(plr.Character.HumanoidRootPart.CFrame.Position.X, plr.Character.HumanoidRootPart.CFrame.Position.Y + 7.5, plr.Character.HumanoidRootPart.CFrame.Position.Z)
elseif submerge then
boat:SetPrimaryPartCFrame(CFrame.new(boat.PrimaryPart.CFrame.Position.X, -5.5, boat.PrimaryPart.CFrame.Position.Z))
end
end
end)
I thought of a way to fix it, and I came up with this:
thing.CFrame.lookVector
so the tween would be
boat_tweens[boat.Name] = TS:Create(boat.PrimaryPart, TInfo, {
CFrame = CFrame.new(boat.PrimaryPart.CFrame.Position + Vector3.new(200,0,0)) * CFrame.Angles(boat.PrimaryPart.CFrame.lookVector)
})
but it throws me an error:
Unable to cast Vector3 to float
Please help!