VehicleSeat:Sit() asks for a string

Hello!
I’m currently working on a GUI Button that, when clicked, Fires a RemoteEvent to a ServerSide Script. The ServerSide Script spawns a Car into the Workspace, and is supposed to force the Player to Sit() into the Car’s DriveSeat.
I’ve literally just learnt how to use VehicleSeat:Sit()… So here’s my code:

-- ServerSide Script (No need for the LocalScript, irrelevant)
local rStorage = game:GetService("ReplicatedStorage")
local sStorage = game:GetService("ServerStorage")
local carAssign = Instance.new("RemoteEvent", rStorage)
local startingGrid = game.Workspace["Starting Grid"]
local bestAvailable = 1

carAssign.Name = "CarAssign"

local function assignCar(plrName, teamName)
	local clone = sStorage.Formula1[teamName]:Clone()
	clone:MoveTo(startingGrid["Position " .. tostring(bestAvailable)].Position)
	clone:PivotTo(startingGrid["Position " .. tostring(bestAvailable)].CFrame)
	clone.Parent = game.Workspace["MWR F1 2022"]
	-- Here is where I sit the Humanoid, but an error comes back saying "Workspace.RaceModeSystem:14: invalid argument #2 (string expected, got Instance)"
clone:WaitForChild("DriveSeat"):Sit(game.Workspace[plrName]:WaitForChild("Humanoid"))
	game.Workspace.Teams[teamName].Value = game.Workspace.Teams[teamName].Value + 1
	startingGrid["GridSlot" .. tostring(bestAvailable)].Occupied = true
	startingGrid["GridSlot" .. tostring(bestAvailable)].DriverName = plrName
	startingGrid["GridSlot" .. tostring(bestAvailable)].TeamName = teamName
	bestAvailable = bestAvailable + 1
end

carAssign.OnServerEvent:Connect(assignCar)

I would be grateful for any help.

May you show the output of this script?

Pretty much thats what he saying.

You should never have concatenated functions without checking return conditions!

local seat = clone:WaitForChild("DriveSeat",3); -- second param will give a delay time (3 seconds) for replication to happen
local player = seat and game.Workspace:FindFirstChild("PlayerName");
local humanoid = player and player:WaitForChild("Humanoid",3);
if not humanoid then "RunAway!" end;

seat:Sit(humanoid);

Change this code to this:

local function assignCar(player, teamName)
	local plrName = player.Name

The error is not caused by the vehicle seat, it is caused by indexing an instance with an instance, when it should be a string.

Yep, first param to OnServerEvent will always be a player instance, so you need to break the name out of the instance. Hence why I simply put “PlayerName” in the logic of finding the player in the Workspace, rather than that which is sent by the RemoteEvent!

Oh… I’m going to try it out, but in the meanwhile I’ll put that as solution, thank you very much to all three.

1 Like

Yes yes, I knew all that, I’m not really a total beginner, but thank you still.

One question though @bluebxrrybot , is the player sent in the FireServer() function the actual player or the player.Character?

OnServerEvent always has the Player object as the first argument. So you don’t need to send the player through FireServer if that’s what you were wondering.

1 Like

Oh wait no that’s stupid it’s obviously the Player sorry.

Ye, it’s automatically sent. No need to provide as a parameter.

1 Like

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