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)
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);
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!
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.