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:
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)
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)
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)