Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Whats the problem with this vehicle spawning system?

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

✓ Accepted answer

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)

1 comment

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.