Transferring variables from local script to server script

I’m trying to make a loadout system for my upcoming game, anyway the first loadout system I made didn’t work because it used remote events to choose which items are being used and it would run a function which gave everyone those guns when the player click play.

Here I attempted to just send the data to the server on which items to give the player when they click play so everyone can have different items.

I’m not great with scripting so I hope some of you who are much smarter can help me out. I’m not sure if I need a module script, or messed up the parameters, or just messed up everything at once.

-- local script

local primary 
local secondary

script.Parent.Snipers.DM7.MouseButton1Click:Connect(function()
	primary = game.ReplicatedStorage.Weapons.DM7
end)

script.Parent.Pistols.X16.MouseButton1Click:Connect(function()
	secondary = game.ReplicatedStorage.Weapons.X16
end)

script.Parent.Parent.PlayButton.MouseButton1Click:Connect(function(player)
	game.ReplicatedStorage.RemoteEvents.RemoteEventPlay:FireServer(primary, secondary, player)
end)



-- server script
game.ReplicatedStorage.RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(function(primary, secondary, player)
		local clone = primary:Clone()
		local clone1 = secondary:Clone()
		clone.Parent = player:WaitForChild("Backpack")
		clone1.Parent = player:WaitForChild("Backpack")
end)

Any and all help is appreciated!

Player is actually passed in as the first argument so this should work:

game.ReplicatedStorage.RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(function(player, primary, secondary)
		local clone = primary:Clone()
		local clone1 = secondary:Clone()
		clone.Parent = player:WaitForChild("StarterGear")
		clone1.Parent = player:WaitForChild("StarterGear")
end)

Unfortunately this still didn’t work.

Sorry, I just edited it, try the code again.

Getting the error “attempt to index nil with ‘Clone’” on line 2

Try this

local primary 
local secondary

script.Parent.Snipers.DM7.MouseButton1Click:Connect(function()
	primary = game.ReplicatedStorage.Weapons.DM7
end)

script.Parent.Pistols.X16.MouseButton1Click:Connect(function()
	secondary = game.ReplicatedStorage.Weapons.X16
end)

script.Parent.Parent.PlayButton.MouseButton1Click:Connect(function(player)
	game.ReplicatedStorage.RemoteEvents.RemoteEventPlay:FireServer(primary, secondary)
end)
-- server script
game.ReplicatedStorage.RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(function(Player, primary, secondary)
	local clone = primary:Clone()
	local clone1 = secondary:Clone()
	clone.Parent = Player:WaitForChild("Backpack")
	clone1.Parent = Player:WaitForChild("Backpack")
end)

When firing a remote to the server there is no need to put the player as an argument because OnServerEvent already takes the player as the first parameter

Getting the error attempt to index nil with ‘Clone’ on line 2

It’s probably because you sent the weapon model through a remote event. Try sending the name of the primary and secondary weapon then finding and cloning it from the server

-- local script
local primary 
local secondary

script.Parent.Snipers.DM7.MouseButton1Click:Connect(function()
	primary = 'DM7'
end)

script.Parent.Pistols.X16.MouseButton1Click:Connect(function()
	secondary = 'X16'
end)

script.Parent.Parent.PlayButton.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEvents.RemoteEventPlay:FireServer(primary, secondary)
end)
-- server script
game.ReplicatedStorage.RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(function(Player, primary, secondary)
	local clone = game.ReplicatedStorage.Weapons:FindFirstChild(primary):Clone()
	local clone1 = game.ReplicatedStorage.Weapons:FindFirstChild(secondary):Clone()
	clone.Parent = Player:WaitForChild("Backpack")
	clone1.Parent = Player:WaitForChild("Backpack")
end)

Clone error is gone, but another error popped up “Argument 1 missing or nil” in the server script.

Client Side

--Services //--
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--Variables //--
local RemoteEvents = ReplicatedStorage:WaitForChild('RemoteEvents')
local primary
local secondary

--Functions //--
script.Parent.Snipers.DM7.MouseButton1Click:Connect(function()
	primary = 'DM7'
end)

script.Parent.Pistols.X16.MouseButton1Click:Connect(function()
	secondary = 'X16'
end)

script.Parent.Parent.PlayButton.MouseButton1Click:Connect(function()
	RemoteEvents.RemoteEventPlay:FireServer(primary, secondary)
end)

Server Side

--Services //--
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--Variables //--
local Weapons = ReplicatedStorage:WaitForChild('Weapons')
local RemoteEvents = ReplicatedStorage:WaitForChild('RemoteEvents')

--Functions //--
RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(function(Player, primary, secondary)
	local Backpack = Player:WaitForChild('Backpack')
	
	local Primary = Weapons:FindFirstChild(primary)
	local Secondary = Weapons:FindFirstChild(secondary)
	
	if Primary then
		Primary = Primary:Clone()
		Primary.Parent = Backpack
	end
	if Secondary then
		Secondary = Secondary:Clone()
		secondary.Parent = Backpack
	end
end)

Looks like it works expect the player isn’t receiving the secondary and and error is popping up “Argument 1 missing or nil - Server - WeaponEquip:11”

xd its because the secondary variable in the client is probably nil which means youre sending over nothing to the server

That’s strange because I’m equipping the secondary, and its being sent with the remote event.

then the primary is probably nil