So I have a problem when trying to get the player’s equipped vehicle because the DataStore2 doesn’t load fast enough. I have to add a wait for it to work properly.
local ServerScriptService = game:GetService("ServerScriptService")
local Saver = ServerScriptService:WaitForChild("Saver")
local DataStore2 = require(Saver:WaitForChild("DataStore2"))
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local VehiclesFolder = ServerStorage:WaitForChild("VehiclesFolder")
local PlayerVehicles = workspace:WaitForChild("PlayerVehicles")
local Checkpoints = workspace:WaitForChild("Checkpoints")
local function sitInVehicle(vehicle, character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
local Body = vehicle:WaitForChild("Body")
local VehicleSeat = Body:WaitForChild("VehicleSeat")
task.wait(1)
VehicleSeat:Sit(humanoid)
end
end
local function spawnEquippedVehicle(player, character)
local Stage = player:WaitForChild("leaderstats"):WaitForChild("Stage") -- number
local EquippedVehicle = player:WaitForChild("EquippedVehicle") -- string
local vehicle = VehiclesFolder:FindFirstChild(EquippedVehicle.Value) -- nil
local checkpoint = Checkpoints:FindFirstChild(Stage.Value) -- nil
local playerVehiclesFolder = PlayerVehicles:FindFirstChild(player.Name)
if vehicle and checkpoint and playerVehiclesFolder then
local SpawnPart = checkpoint:WaitForChild("SpawnPart")
local newVehicle = vehicle:Clone()
newVehicle:PivotTo(SpawnPart.CFrame + Vector3.new(0, 5, 0))
newVehicle.Parent = playerVehiclesFolder
sitInVehicle(newVehicle, character)
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
RunService.Heartbeat:Wait()
task.wait(2)
spawnEquippedVehicle(player, character)
end)
end)
Any better way to solve this?