Help with car spawner

You can write your topic however you want, but you need to answer these questions:
Hi guys, well, I have a car spawner and it works, but not as well as I would like, so I decided to create a new one based on events. The problem is that I know almost nothing about events, and even reading a lot, I still don’t understand very well. The old script works like this:

local blueford= script.Parent.FrameColors.Fordka.Blue

blueford.MouseButton1Click:Connect(function() 
	    if clone then 
		   clone:Destroy() 
		 Mod = game.ServerStorage.Carros.FordKa.FordKaAzul   --muda
         clone = game.Players.Name
         clone = Mod:clone()
         clone.Parent = workspace
         clone:MakeJoints()
	 elseif not clone then
         Mod = game.ServerStorage.Carros.FordKa.FordKaAzul
         clone = game.Players.Name
         clone = Mod:clone()
         clone.Parent = workspace
         clone:MakeJoints()
  end
end)

It works, I repeat it a lot of times inside one main script, once per color, but when I reach 2579 lines, it doesn’t work. another problem with this script is that it can’t remove the car when the player leaves for example. So I thought about dividing the scripts one in each color button, and linking them by events, to know the player’s name, so I did the following:

(localscript inside the color button)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("EventoRemotoDeCarro")

script.Parent.MouseButton1Click:Connect(function()

remoteEvent:FireServer(game.ServerStorage.fordfusion.BlueFord)

end)

(serverscript)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("EventoRemotoDeCarro")

local playername = script.Parent.playername

local function CarRequested(player, car)

playername.Value = player.Name

carroingame = game.Workspace:FindFirstChild(playername.Value.."'s Car")

if carroingame then

carroingame:Destroy()

end

Mod = car -- muda

clone = Mod:clone()

clone.Parent = workspace

clone:MakeJoints()

clone.Name = (playername.Value.."'s Car")

end

remoteEvent.OnServerEvent:Connect(CarRequested)

It is not working, and how I said, I’m not used to script using events, so I can’t find what is making it doesn’t work, I’ve checked the output, but it doesn’t report any error.

1 Like

Hi! I think i fixed it! Here is the code for the serverscript bc the local script was fine:

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local remoteEvent = ReplicatedStorage:WaitForChild(“EventoRemotoDeCarro”)
local playername = script.Parent.playername

remoteEvent.OnServerEvent:Connect(function(player, car)
playername.Value = player.Name
local carroingame = game.Workspace:FindFirstChild(playername.Value…“'s Car”)

if carroingame then
	carroingame:Destroy()
end

local Mod = car -- muda
local clone = Mod:clone()

clone.Parent = workspace
clone:MakeJoints()
clone.Name = (playername.Value.."'s Car")

end)