Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Whats the problem with this vehicle spawning system?

Asked by 2 years ago

Hello, I'm making a vehicle game and I have it so once a player joins the game it fires a remote event that spawns a car and teleports that player to the car but if someone joins after me they cannot see my car but instead see me flying around sitting in the air yet I see their car. Is it because they weren't there for when I fired the remote event to spawn my vehicle? or is it a different issue entirely? and can you guys give me any ideas on how to fix it?

Local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("SpawnCar")

remoteEvent:FireServer()

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local spawns = workspace.Spawns:GetChildren()

local function onCreateCar(player)
    local randomSpawn = spawns[math.random(#spawns)]
    print(player.Name, "has requested a vehicle")

    local car = ReplicatedStorage:WaitForChild("Car")
    local carClone = car:Clone()

    car.Parent = workspace
    car:SetPrimaryPartCFrame(randomSpawn.CFrame)
    game["Run Service"].Heartbeat:Wait()
    player.Character.HumanoidRootPart.Position = car.VehicleSeat.Position
    game["Run Service"].Heartbeat:Wait()
    car.VehicleSeat:Sit(player.Character.Humanoid)
end

remoteEvent.OnServerEvent:Connect(onCreateCar)

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Use Players.PlayerAdded instead. Also in lines 13-18, you're using the car from ReplicatedStorage and not the clone itself, that's why not all players can see your vehicle.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spawns = workspace.Spawns:GetChildren()

Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local randomSpawn = spawns[Random.new(os.time()):NextInteger(1, #spawns)]
        print(Player.Name .. " has requested a vehicle")

        local car = ReplicatedStorage:WaitForChild("Car")
        local carClone = car:Clone()

        carClone.Parent = workspace
        carClone:PivotTo(randomSpawn:GetPivot())
        task.wait()
        Character:PivotTo(carClone.VehicleSeat.CFrame)
        task.wait()
        carClone.VehicleSeat:Sit(Character:WaitForChild("Humanoid"))
    end
end)
0
Thanks! I originally used Players.PlayAdded but it didnt work, assuming because I was using the car from ReplicatedStorage. I've yet to test this with other real players yet but it works in studio so hopefully its fixed! MattWasTaken96 18 — 2y
Ad

Answer this question