How do I make it so this car spawning script triggers whenever a player says a command

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

player.Chatted:Connect(function(msg)
       if msg == "Hello World!" then
             print("Wow")
       end
end)
1 Like

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)
1 Like

Try this out,
replace this script with yours

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)

I got a new output:
ServerScriptService.Script:4: Expected ‘)’ (to close ‘(’ at line 3), got 'v1’

give me a min, ill check and let you know

I guess I have fixed it, check it out now.

Here is the new code

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)

Another output:
ServerScriptService.Script:6: Expected identifier when parsing expression, got 'local’

give me a min, idk why. ill get back asap

I added the " " before this once I found out in the output.

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.

alr check this out

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)
1 Like

this could do but there are 2 scripts and it’s lengthy.

That is true.

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

I guess spawning the car in server script wouldn’t cause any error
anyways @grimmerschool2 look for the script that works and use it

1 Like

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)

Still doesn’t work. The output says its from the “local” in line 6.

btw I forgot to say you to relocate the cars folder from server storage to replicated storage

I have used replicated storage there and not server storage