Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I fire a RemoteEvent to spawn a specific vehicle that was clicked?

Hello! So today I figured out a script that spawns a vehicle from a folder of vehicles you own, except the only problem is that it's Local-Sided. How would I fire a RemoteEvent from a script to spawn it if it needs specific information? I tried but it keeps saying "Argument 1 is missing or nil". The script creates textbuttons in a GUI and they spawn a vehicle once clicked. This is in a LocalScript in the gui. I'm just confused on how I would give the remote event the information.

This is my attempt at using the RemoteEvent on line 49-

Here's the Local-Script

-- Vehicles

local player = game.Players.LocalPlayer
local character = player.Character
local cars = {}
local buttons2 = {}

local spawncar = game.ReplicatedStorage.Remotes.Vehicle.AddCar
local deletecar = game.ReplicatedStorage.Remotes.Vehicle.DestroyCar

local location = game.Players.LocalPlayer:WaitForChild("items"):WaitForChild("Vehicles")
function search(location)
	for i,v in pairs(location:GetChildren()) do -- Find all item in a specific location
		if v:isA("Model") then
			table.insert(cars,v) -- We're going to put all the vehicles found in a table.
		end
	end
end

function refresh()
	for i,v in pairs(buttons2) do -- Finds all cars in the table
		v:Destroy() -- Destroys them
	end
	
	local location = script.Parent.Handler2.Location
	local selected = script.Parent.Handler2.Selected

	
	for i,v in pairs(cars) do
		local button = script.Sample:Clone()
		button.Name = v.Name
		button.LayoutOrder = i
		button.Parent = script.Parent.Handler2
		--button.Image = v.TextureId
		table.insert(buttons2,button) 
		
		button.MouseButton1Click:Connect(function()

		local cl0ne = v:Clone()
		cl0ne.Name = player.Name.."'s Car"
			
			if workspace:FindFirstChild(player.Name.."'s Car") ~= true then -- if false
			if selected.Value == nil or selected.Value ~= v and location.Value ~= workspace or location.Value == nil then
					
					spawncar:FireServer() -- Fire the remote event
					
					
					location.Value = v.Parent
					selected.Value = v 
					
					
			else
					if workspace:FindFirstChild(player.Name.."'s Car") ~= nil then -- if true
						
						for _, Model in pairs(workspace:GetChildren()) do
   						 if Model.Name == (player.Name.."'s Car") then
							Model:Destroy()
					print("Despawning Successful")
					selected.Value = nil
					location.Value = nil
					
					-- spawn car
					-- delete car
					
					end
					end
					end
					end
			end
		end)
	end
end		



function backpackRefresh()
	cars = {}
	search(character)
	search(player.items.Vehicles)
	refresh()
end

backpackRefresh()

player.items.Vehicles.ChildAdded:Connect(backpackRefresh)
player.items.Vehicles.ChildRemoved:Connect(backpackRefresh)

character.ChildAdded:Connect(backpackRefresh)
character.ChildRemoved:Connect(backpackRefresh)

The regular script in ServerScriptService:

game.ReplicatedStorage.Remotes.Vehicle.AddCar.OnServerEvent:Connect(function(player,carName)
	local vehicle = player:FindFirstChild("items"):FindFirstChild("Vehicles"):FindFirstChild(carName):Clone()
	vehicle.Parent = workspace
	vehicle:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,5,0))
	vehicle:MakeJoints()
end)

2 comments

0

use bindable event idiot

0

it works fine with remoteevent. also be polite to others on here...

1 answer

✓ Accepted answer

Just pass the value to the carName argument. Like this:

spawnCar:FireServer(v.Name) -- then the server will know what carName is

1 comment

0

I already figured it out and forgot to write it as solved, sorry! Thanks for the help though!

This discussion is preserved as it was. New answers, comments, and votes are closed.