Spawn Player's Car and Sit Player on Their Car doesn't Work

I just wanted to know if you can sit a player into their own car.

I wanted to make a script that when a player is added it will spawn a new car for the player and sit them.

But instead it makes this result when trying it out on a Multiplayer Test:
image

image

Here’s the code:

local SS = game:GetService("ServerStorage")
local car = SS.Car -- path to car model
game.Players.PlayerAdded:Connect(function(player) -- detect player joining
	local cloned = car:Clone() -- clone car
	cloned.Name = player.Name .. "‘s Vehicle"
	print("Wait for the character to be added.")
	player.CharacterAdded:Wait()
	charVariable = player.Character
	print("Alright!")
	cloned.Parent = game.Workspace -- parent vehicle to Workspace
	print("Sitting player to their car...")
	cloned.DriveSeat:Sit(charVariable.Humanoid)
end)
2 Likes

It could be a problem with the character not loading in time.

Fixed code:

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--//Variables
local Car = ServerStorage.Car

--//Functions
Players.PlayerAdded:Connect(function(player) -- detect player joining
	local newCar = Car:Clone()
	newCar.Name = player.Name .."'s Vehicle"
	newCar.Parent = workspace
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		newCar.DriveSeat:Sit(humanoid)
	end)
end)

Tell me if there are any errors.

1 Like

image
Still the same… :confused:
I am also gonna tell you that my script is located in ServerScriptService.

1 Like

Can you input this code and tell me what it prints?

local SS = game:GetService("ServerStorage")
local car = SS.Car -- path to car model
game.Players.PlayerAdded:Connect(function(player) -- detect player joining
	local cloned = car:Clone() -- clone car
	cloned.Name = player.Name .. "‘s Vehicle"
	print("Wait for the character to be added.")
	player.CharacterAdded:Wait()
	charVariable = player.Character
	print("Alright!")
	cloned.Parent = game.Workspace -- parent vehicle to Workspace
	print("Sitting player to their car...")
    print(charVariable.Humanoid.ClassName) print(typeof(charVariable.Humanoid))
	cloned.DriveSeat:Sit(charVariable.Humanoid)
end)
1 Like

It prints this, But still…
image

1 Like

Are you sure the humanoid is alive?

1 Like

Found out the bug, you need to add a wait.

Fixed code:

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--//Variables
local Car = ServerStorage.Car

--//Functions
Players.PlayerAdded:Connect(function(player) -- detect player joining
	local newCar = Car:Clone()
	newCar.Name = player.Name .."'s Vehicle"
	newCar.Parent = workspace
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		task.wait()
		newCar.DriveSeat:Sit(humanoid)
	end)
end)
1 Like

Yes i checked, Is alive, But i think it’s probably thinking that is dead or does not exist at all, even tho the humanoid spawns.

1 Like

It works and all, but why does this happen to my car???

I don’t think is probably a bug but still a problem…

The code for the car:

local car = script.Parent
local stats = car.Configurations
local Raycast = require(script.RaycastModule)

local mass = 0

for i, v in pairs(car:GetChildren()) do
	if v:IsA("BasePart") then
		mass = mass + (v:GetMass() * 196.2)
	end
end

local bodyPosition = Instance.new("BodyPosition", car.Chassis)
bodyPosition.MaxForce = Vector3.new()
local bodyGyro = Instance.new("BodyGyro", car.Chassis)
bodyGyro.MaxTorque = Vector3.new()

local function UpdateThruster(thruster)
	-- Raycasting
	local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value) --game.Workspace:FindPartOnRay(ray, car)
	local thrusterHeight = (position - thruster.Position).magnitude
	
	-- Wheel
	local wheelWeld = thruster:FindFirstChild("WheelWeld")
	wheelWeld.C0 = CFrame.new(0, -math.min(thrusterHeight, stats.Height.Value * 0.8) + (wheelWeld.Part1.Size.Y / 2), 0)
	-- Wheel turning
	local offset = car.Chassis.CFrame:inverse() * thruster.CFrame
	local speed = car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)
	if offset.Z < 0 then
		local direction = 1
		if speed.Z > 0 then
			direction = -1
		end
		wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(0, (car.Chassis.RotVelocity.Y / 2) * direction, 0)
	end
	
	script.Parent.Chassis.engine.PlaybackSpeed = thruster.Velocity.magnitude / 55
	
	if thruster.Velocity.magnitude <= 15 then
		script.Parent.Chassis.engine.PlaybackSpeed = 0.3
	end
	
	-- Particles
	if hit and thruster.Velocity.magnitude >= 5 then
		wheelWeld.Part1.ParticleEmitter.Enabled = true
	else
		wheelWeld.Part1.ParticleEmitter.Enabled = false
	end
end

car.DriveSeat.Changed:connect(function(property)
	if property == "Occupant" then
		if car.DriveSeat.Occupant then
			local player = game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)
			if player then
				car.DriveSeat:SetNetworkOwner(player)
				local localCarScript = script.LocalCarScript:Clone()
				localCarScript.Parent = player.PlayerGui
				localCarScript.Car.Value = car
				localCarScript.Disabled = false
			end
		end
	end
end)

spawn(function()
	while true do
		game:GetService("RunService").Stepped:wait()
		for i, part in pairs(car:GetChildren()) do
			if part.Name == "Thruster" then
				UpdateThruster(part)
			end
		end
		if car.DriveSeat.Occupant then
			bodyPosition.MaxForce = Vector3.new()
			bodyGyro.MaxTorque = Vector3.new()
		else
			local hit, position, normal = Raycast.new(car.Chassis.Position, car.Chassis.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)
			if hit and hit.CanCollide then
				bodyPosition.MaxForce = Vector3.new(mass / 5, math.huge, mass / 5)
				bodyPosition.Position = (CFrame.new(position, position + normal) * CFrame.new(0, 0, -stats.Height.Value + 0.5)).p
				bodyGyro.MaxTorque = Vector3.new(math.huge, 0, math.huge)
				bodyGyro.CFrame = CFrame.new(position, position + normal) * CFrame.Angles(-math.pi/2, 0, 0)
			else
				bodyPosition.MaxForce = Vector3.new()
				bodyGyro.MaxTorque = Vector3.new()
			end
		end
	end
end)

Put your car in ReplicatedStorage before cloning it, this is probably a replication bug.

1 Like

I tried to do everything, I pretty don’t know much about ReplicatedStorage. (is pretty new to me.)

i tried putting it in the ReplictedStorage once it was starting to clone, and i still couldn’t get it to work.

That’s not what I meant, I’ll just give you the steps then.

  1. Put your car model into ReplicatedStorage instead of ServerStorage.
  2. Change your script to this:
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Car = ReplicatedStorage.Car

--//Functions
Players.PlayerAdded:Connect(function(player) -- detect player joining
	local newCar = Car:Clone()
	newCar.Name = player.Name .."'s Vehicle"
	newCar.Parent = workspace
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		task.wait()
		newCar.DriveSeat:Sit(humanoid)
	end)
end)

I think i made it worst.
My character now moves…
Is probably something else, Or am i forgetting something important…


image

Did you put the car into ReplicatedStorage before you started the game? If you could just send me the place file it would be way easier to help.

Alright.
Car Game wip.rbxl (258.7 KB)

Try this:
Car Game wip.rbxl (258.3 KB)

Thank you, I also tested it on Multiplayer, It works perfectly!

1 Like

No problem. If you have any other questions, feel free to ask.

1 Like

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