Hello. I was scripting a command-operated car spawning script. While trying to do that, I ran into problems (of course I always do when scripting) while trying to script.
First, I don’t know how to make the script chat-operated. Second, I want to make it so only certain players can use this command.
Script:
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
SpawnCarEvent.OnServerEvent:Connect(function(player, carName)
local Car = ServerStorage:FindFirstChild("Cars"):FindFirstChild(carName)
if Car then
local clonedCar = Car:Clone()
clonedCar.Name = player.Name .. 'sCar'
clonedCar.Parent = game.Workspace
clonedCar:MoveTo -- uh idk what to do here
end
end)
This is how the car spawning should work:
Orange: Car
Green: Player
Tried your solution, got a output: ServerScriptService.Script:6: attempt to index nil with 'Chatted’
Script:
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local pos1 = game.ServerStorage.vehicles.Jeep.playerpos
player.Chatted:Connect(function(msg)
if msg == "!spawncar jeep_old" then
local Car = ServerStorage:FindFirstChild("vehicles"):FindFirstChild("Jeep")
if Car then
local clonedCar = Car:Clone()
clonedCar.Name = player.Name .. 'sCar'
clonedCar.Parent = game.Workspace
clonedCar.playerpos:MoveTo(player.Character.HumanoidRootPart.Position)
end
end
end)
local AllowSpawnCar = {} -- fill this table with user ids of player who can spawn car
local Command = /SpawnCar -- replace this with your command
game.Players.PlayerAdded:Connect(function(v1)
v1.Chatted:Connect(function(v2)
if table.find(AllowSpawnCar, v1.UserId) ~= nil and if string.lower(v2) == string.lower(Command) then
local clonedCar = game:GetService("ReplicatedStorage"):WaitForChild("Cars"):FindFirstChild("Car"):Clone()
clonedCar.Name = player.Name .. 'sCar'
clonedCar.Parent = game.Workspace
clonedCar.playerpos:MoveTo(player.Character.HumanoidRootPart.Position)
end
end)
end)
local AllowSpawnCar = {} -- fill this table with user ids of player who can spawn car
local Command = /SpawnCar -- replace this with your command
game.Players.PlayerAdded:Connect(function(v1)
v1.Chatted:Connect(function(v2)
if table.find(AllowSpawnCar, v1.UserId) ~= nil and if string.lower(v2) == string.lower(Command) then
local clonedCar = game:GetService("ReplicatedStorage"):WaitForChild("Cars"):FindFirstChild("Car"):Clone()
clonedCar.Name = player.Name .. 'sCar'
clonedCar.Parent = game.Workspace
clonedCar.playerpos:MoveTo(player.Character.HumanoidRootPart.Position)
end
end)
end)
player is undefined, and therefore nil. You may have to define player by LocalScript and then send the function through a RemoteEvent.
LocalScript:
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local pos1 = game.ServerStorage.vehicles.Jeep.playerpos
local player = game.Players.LocalPlayer
local event = ReplicatedStorage.spawnevent
player.Chatted:Connect(function(msg)
if msg == "!spawncar jeep_old" then
event.FireServer("jeep_old")
end
end)
Script:
game.ReplicatedStorage.event.OnServerEvent:Connect(function(player, car)
if car == "jeep_old" then
local Car = ServerStorage:FindFirstChild("vehicles"):FindFirstChild("Jeep")
if Car then
local clonedCar = Car:Clone()
clonedCar.Name = player.Name .. 'sCar'
clonedCar.Parent = game.Workspace
clonedCar.playerpos:MoveTo(player.Character.HumanoidRootPart.Position)
end
elseif car == "sedan" then
--other car code i guess, just wanted to put it here
end
end)
I haven’t developed in Studio for a while, but this should work.
Feel free to edit it if I did something wrong.
local AllowSpawnCar = {} -- fill this table with user ids of player who can spawn car
local Command = "/SpawnCar" -- replace this with your command
game.Players.PlayerAdded:Connect(function(v1)
v1.Chatted:Connect(function(v2)
if table.find(AllowSpawnCar, v1.UserId) ~= nil and string.lower(v2) == string.lower(Command) then
local clonedCar = game:GetService("ReplicatedStorage"):WaitForChild("Cars"):FindFirstChild("Car"):Clone()
clonedCar.Name = v1.Name .. 'sCar'
clonedCar.Parent = game.Workspace
clonedCar.playerpos:MoveTo(v1.Character.HumanoidRootPart.Position)
end
end)
end)
However, if you were to run everything in a LocalScript specifically, the car wouldn’t render for others.
And, if you were to run everything in a Script specifically, while the car would work fine, it take more than necessary code to make it work.
This is my personal opinion based on preference and simplicity (possibly convenience).
Try this, make sure to edit the table for the cars.
And set the primary part for the cars.
task.wait()
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Command = "/spawn"
local cars = {
["sCar"] = ServerStorage:WaitForChild("") -- Input your directory.
}
local function GetCharacter(plr : Instance)
local character = plr.Character
if character then
return character
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
local args = string.split(msg, " ")
if string.lower(args[1]) == Command then
for n, v in next, cars do
if string.lower(args[2]) == n then
local Car = v:Clone()
if Car then
local Character = GetCharacter(plr)
Car.Name = plr.Name .. n
Car.Parent = game.Workspace
if Character then
if Character:FindFirstChild("HumanoidRootPart") then
Car:SetPrimaryPartCFrame(CFrame.new(Character.HumanoidRootPart.LookVector * 5))
end
end
end
end
end
end
end)
end)