My issue is that my script here:
local Players = game:GetService(“Players”)
Players.PlayerAdded:Connect(function(Carplayer)
function Click()
if workspace.Spawn1.CARHERE.Value == true then
caridiot1 = game.ServerStorage[“Suburban”]:Clone()-- Only part that should be edited in this script is suburban
caridiot1.Name = Carplayer.Name…“Car”
caridiot1.Parent = workspace
end – Change ‘Suburban’ to your vehicle name.
end
end)
script.Parent.MouseButton1Down:connect(Click)
Won’t even work. It keeps on calling nil value. this is a gui car spawner and I don’t know why its not working. HELP FAST
If it’s a serverscript in a GUI then it won’t work. Use RemoteFunctions or RemoteEvents to have client/server interaction. Here’s a YT link that helped me when I was getting started with that stuff;
This is because Click() is enclosed within an event, and not in the global scope.
You don’t seem to be using the event already. To fix this, simply remove it.
function Click()
if workspace.Spawn1.CARHERE.Value == true then
caridiot1 = game.ServerStorage[“Suburban”]:Clone()-- Only part that should be edited in this script is suburban
caridiot1.Name = Carplayer.Name…“Car”
caridiot1.Parent = workspace
end – Change ‘Suburban’ to your vehicle name.
end
script.Parent.MouseButton1Down:connect(Click)
Also, next time, please try to format your code. Use three ` at the start of the code, and again in an end line to do this.
Looks like you’ve got quite a lot of problems here, and it isn’t just the fact that you’re using a single local script to get items from ServerStorage (which does not replicate to clients)
I recommend going on the devwiki and running through some tutorials so you get a better understanding, or maybe watching some AlvinBlox (his tutorials are pretty good and helped me when I was starting)
Anyways, I would do the following for this
Script in ServerScriptService :
local cooldown_time = 10 -- Time until they can spawn another car
local car = game:GetService("ServerStorage"):WaitForChild("Suburban")
local remote = Instance.new("RemoteEvent") -- Remote events allow players to send messages to the server
remote.Name = "SpawnCarRemote"
remote.Parent = game:GetService("ReplicatedStorage")
local cooldown = {} -- This will let us stop players from spamming the button
remote.OnServerEvent:Connect(function(player)
if not player.Character then return end
if cooldown[player] then return end -- If they're on the cooldown list, don't spawn a car
cooldown[player] = true -- Adds them to the cooldown list
local currentcar = car:Clone() -- Create a copy of the car to move
currentcar:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(6,0,0)) -- Move the car 6 studs away from them
currentcar.Parent = workspace -- Make it so players can see the car
wait(cooldown_time)
cooldown[player] = nil
end)
Script in a button inside StarterGui (example below) :
local remote = game:GetService("ReplicatedStorage"):WaitForChild("SpawnCarRemote",5)
if not remote then
warn("SpawnCarRemote not found in ReplicatedStorage")
return
end
script.Parent.MouseButton1Click:Connect(function()
remote:FireServer() -- Sends a message to the server to spawn a car
end)