Button that spawns a car, won't spawn a car

I want to make a car spawner for my game, so the player can easily go about the map, but for some reason it won’t work.

local car = game.Workspace.Cartemplate

script.Parent.MouseClick:Connect(function(player)
	local carClone = car:Clone()
	carClone.Parent = game.Workspace
	print("Success!")
end)

I tried to use the AI Assistent to help me, but it still won’t work.

What did I do wrong?

4 Likes

The issue is that you didn’t put a position for the clone where the car is supposed to spawn, I fixed this for you.

local car = game.Workspace.Cartemplate

script.Parent.MouseClick:Connect(function(player)
	local carClone = car:Clone()
	carClone.Parent = game.Workspace
        carClone.CFrame = script.Parent.CFrame
	print("Success!")
end)

Hope this helps or let me know is doesnt work

4 Likes

It didn’t work still, but my friend has come with a solution already, thanks for helping!

Useful if you’d post it here so that others who have this problem can see the solution.

It doesn’t work anymore? But I swear this worked yesterday…

local car = game.ServerStorage.Vehicles.Sudan
local OFFSET = Vector3.new(20, 0, 0)
local player = game.Players.LocalPlayer
local character = player.Character
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local targetPosition = humanoidRootPart.Position + OFFSET

script.Parent.MouseClick:Connect(function(player)
	local carClone = car:Clone()
	carClone.Parent = game.Workspace
	carClone.CFrame = CFrame.new(targetPosition)
	print("Success!")
end)

Are you coding that on client or server script?

Judging off the fact you’re using MouseClick and LocalPlayer I can only assume you’re running this on the client.

The client doesn’t have access to ServerStorage, so it’s not able to access the car.

The solution would be to either store the car in the ReplicatedStorage, or fire a remote event that clones the car on the server when the player clicks.

Why does the function have a player as a parameter,it confuses with yhe player varaible,resulting the code to be not working