I am making a system that spawns a player's chosen vehicle into the game. It also forces the player to sit in the vehicle.
(You may need to click view source to see the full script. Some of it may be cut off.)
Here is the local Script in StarterPlayerScripts:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local Cars = ReplicatedStorage.PlayerCars ReplicatedStorage.PlayerCarEvents.SpawnCar.OnClientEvent:Connect(function(Car, Position) local playerCar = Cars:FindFirstChild(Car):Clone() playerCar.Parent = workspace playerCar:MoveTo(Position) game.Players.LocalPlayer.CharacterAdded:Connect(function() ReplicatedStorage.PlayerCarEvents.SpawnPlayer:FireServer(playerCar.Chassis.VehicleSeat) end) end)
Here is the server script in ServerScriptService:
local DataSotreService = game:GetService("DataStoreService") local PlayerCarDataStore = DataSotreService:GetDataStore("PlayerCar") local DefaultCar = "Pickup Truck (blue)" local ReplicatedSotrage = game:GetService("ReplicatedStorage") local CarSpawns = game.Workspace:FindFirstChild("Track Structures").CarSpawners game.Players.PlayerAdded:Connect(function(Player) for i, carSpawn in ipairs(CarSpawns:GetChildren()) do if carSpawn:GetAttribute("Used") == false then ReplicatedSotrage.PlayerCarEvents.SpawnCar:FireClient(Player, PlayerCarDataStore:GetAsync(Player) or DefaultCar, Vector3.new(carSpawn.Position.X, carSpawn.Position.Y + 15, carSpawn.Position.Z)) carSpawn:SetAttribute("Used", true) game.ReplicatedStorage.PlayerCarEvents.SpawnPlayer.OnServerEvent:Connect(function(Seat, Character) Seat:Sit(Player.Character:WaitForChild("Humanoid")) end) break end end end)
In summary, when the player spawns the server tells the client to spawn the player's car and all the information it needs to spawn it. That part works. But then the client tells the server to force the player to sit in the seat of the car (line 9 of the local script) but the sever keeps outputting the error: 'Sit is not a valid member of Players.InsertThePlayersUsernameHere'. Can somebody help me fix this error?
This is a mistake most people make. To sum it up, basically the first parameter of a firing/invoking Remote Event/Remote Function from Client to server is the player instance that fired it. I don't really know how to explain it, but let me try to make an example.
Client:
Event:FireServer("HI")
Server:
INCORRECT
Event.OnServerEvent:Connect(function(String) print(String) end)
If you put that, you are trying to print the PLAYER instance. RemoteEvents and RemoteFunction put the first parameter as the Player instance who fired the event. But if it's the other way around, like Server firing to Client, then the first parameter wouldn't be the Player instance, but the first tuple argument we've put.
CORRECT
Event.OnServerEvent:Connect(function(plr, String) print(String) end)
So basically, in the script you have, you are trying to use the :Sit() function from the Player instance, which you can't.