Need help cloning gun to StarterGear

I am making a loadout GUI. There are only two teams and each team has four unique loadouts. I am trying to make it so that when you click on the loadout, you get the specific guns entitled to that loadout, which are cloned to the StarterGear.

Basically, when you press the button, it should fire to the remote event in ReplicatedStorage which is connected to the Demolitions script inside ServerScriptStorage, which should clone to the StarterGear, but I’m unaware to how to do that. I also think that I used a few wrong references.

Also, I keep getting this error, “ServerScriptService.Loadout.Demolitions:6: attempt to index nil with ‘StarterGear’”.

In the Loadout script inside the GUI, I have this code.

--Variables
local players = game:GetService("Players");
local player = players.LocalPlayer;

repeat wait(0.01) until player.Character;

local character = player.Character;
local backpack = player:WaitForChild("Backpack");
local humanoid = character:WaitForChild("Humanoid");
local button = script.Parent;
local sidebar = script.Parent.Parent.Parent.Parent:WaitForChild("Sidebar");
local camscript = script.Parent.Parent.Parent.Parent:WaitForChild("CameraOnJoin")
local camera = workspace.CurrentCamera;
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local guns = ReplicatedStorage:WaitForChild("Guns");
local give = {"UMP45","RPG-7"};
local demolitions = game.ReplicatedStorage.Loadout:WaitForChild("Demolitions")

if player.TeamColor ~= BrickColor.new("Navy blue") then
	script.Parent.MouseButton1Click:Connect(function()
		player.TeamColor = BrickColor.new("Navy blue");
		sidebar.Visible = false;
		camera.CameraType = Enum.CameraType.Custom;
		humanoid.Health = 0;
	end);
else
	sidebar.Visible = false;
	camscript.Disabled = true;
end

script.Parent.MouseButton1Click:Connect(function()
	demolitions:FireServer()
	print("Fired")
end)

In the Demoltions script in ServerScript Storage, I have this code.

--Variables
local demolitions = game.ReplicatedStorage.Loadout:FindFirstChild("Demolitions")
local give = {"UMP45","RPG-7"};
local guns = game.ReplicatedStorage:FindFirstChild("Guns")
local plr = game.Players.LocalPlayer
local starterGear = plr.StarterGear
local primary = guns:FindFirstChild("UMP45")
local secondary = guns:FindFirstChild("RPG-7")

print("Initiating")

demolitions.OnServerEvent:Connect(function()
	primary:Clone()
	primary.Parent = starterGear
	print("Cloned Primary")
	secondary:Clone()
	secondary.Parent = starterGear
	print("Cloned Secondary")
end)

I would love to hear your feedback on how to make this work and to maybe polish it up a little.

Server scripts cannot access LocalPlayer, so it becomes nil when referenced by the server. A solution to this problem is using the player parameter OnServerEvent always has.

demolitions.OnServerEvent:Connect(function(player)
    local starterGear = player.StarterGear
	primary:Clone()
	primary.Parent = starterGear
	print("Cloned Primary")
	secondary:Clone()
	secondary.Parent = starterGear
	print("Cloned Secondary")
end)
2 Likes

Another idea could possibly be bypassing the StarterGear entirely? Galaxy is right when he says that StarterGui cannot be accessed by a server side script, but whenever a player spawns in, that can be recognised.

After all, StarterGear waits until the character is spawned, and then clones the contents into the Backpack. Why not cut out the middle man and clone the gear into the backpack directly whenever the player spawns in.

Not sure whether this way is better but perhaps could serve as an alternative.

1 Like

Ok thanks! This worked perfectly.

That sounds like a good alternative, but I don’t think I’m good enough with scripting to be able to do that.

I see you’ve already got a solution but for future reference:

-- In a ServerSideScript
game.Players.PlayerAdded:Connect(function(Player)
     -- Whenever a player connects to your game.
     Player.CharacterAdded:Connect(function(Character)
          -- Whenever the player respawns
          for _, Tool in pairs (Loadout) do
               -- Where "Loadout" is an array of tools that they player will be given.
               -- There are many ways to do this Loadout, could be an array, 
               -- could be ObjectValues somewhere, it's up to you

               -- Clone the tool into the Backpack
               local Clone = Tool:Clone()
               Clone.Parent = Player.Backpack
          end
     end)
end)

Again, not sure whether this is the best solution but certainly would work. This method would work well if you were to implement a custom inventory system and not rely on Roblox’s Backpack system.

1 Like

Ok thanks! I’ll be sure to use this in the future if any other methods seem to fail.

1 Like