Unable to equip weapons for the second time?

Hi Developers!

During the first round every weapon works fine, but in the 2nd round some weapons randomly just don’t work. They exist in Backpack on Client and on Server sides, like it doesn’t want to render or something.

Here are some scripts related to it:

Pick weapons from loadout

local tweenService = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")

local plr = game.Players.LocalPlayer

local weaponsFolder = plr:WaitForChild("Weapons")
local weaponClasses = {weaponsFolder.Historical, weaponsFolder.Unnatural}
local weaponTypes = {weaponsFolder.Historical:GetChildren(), weaponsFolder.Unnatural:GetChildren()}



local loadout = script.Parent:WaitForChild("Loadout")



local primary = nil
local secondary = nil
local melee = nil


local function updateLoadout()
	
	loadout.WeaponsEquipped.Text = ""
	
	if primary ~= nil then
		loadout.WeaponsEquipped.Text = "Primary: " .. primary.Name
		game.ReplicatedStorage.EquipWeaponRE:FireServer(primary)
		
		script.Parent.Parent.Parent.Parent.Primary.Value = primary
		
	else
		return
	end
	
	if secondary ~= nil then
		loadout.WeaponsEquipped.Text = loadout.WeaponsEquipped.Text .. "\nSecondary: " .. secondary.Name
		game.ReplicatedStorage.EquipWeaponRE:FireServer(secondary)
		
		script.Parent.Parent.Parent.Parent.Secondary.Value = secondary
		
	else
		return
	end
	
	if melee ~= nil then
		loadout.WeaponsEquipped.Text = loadout.WeaponsEquipped.Text .. "\nMelee: " .. melee.Name
		game.ReplicatedStorage.EquipWeaponRE:FireServer(melee)
		
		script.Parent.Parent.Parent.Parent.Melee.Value = melee
		
	else
		return
	end
end
updateLoadout()


for i, weaponClass in ipairs(weaponClasses) do
	
	local newButton = script.WeaponClassButton:Clone()
	newButton.Text = weaponClass.Name
	
	newButton.Parent = loadout.WeaponClasses
	
	
	
	
	newButton.MouseButton1Click:Connect(function()
		
		
		for i, child in pairs(loadout.TypesList:GetChildren()) do

			if child:IsA("TextButton") then child:Destroy() end
		end
		
		for i, child in pairs(loadout.WeaponsList:GetChildren()) do

			if child:IsA("TextButton") then child:Destroy() end
		end

		
		for i, weaponType in ipairs(weaponClass:GetChildren()) do
			
			local newTypeButton = script.WeaponTypeButton:Clone()
			newTypeButton.Text = weaponType.Name

			newTypeButton.Parent = loadout.TypesList
			
			

			
			newTypeButton.MouseButton1Click:Connect(function()
				
				
				for i, child in pairs(loadout.WeaponsList:GetChildren()) do

					if child:IsA("TextButton") then child:Destroy() end
				end
				
				for i, weaponChild in ipairs(weaponType:GetChildren()) do

					local newWeaponButton = script.WeaponButton:Clone()
					newWeaponButton.Text = weaponChild.Name
					newWeaponButton.Parent = loadout.WeaponsList



					newWeaponButton.MouseButton1Click:Connect(function()
						
						if weaponType.Name == "Primary" then
							primary = weaponChild
							script.Parent.StrPrimary.Value = weaponChild.Name
							

						elseif weaponType.Name == "Secondary" then
							secondary = weaponChild
							script.Parent.StrSecondary.Value = weaponChild.Name
							

						elseif weaponType.Name == "Melee" then
							melee = weaponChild
							script.Parent.StrMelee.Value = weaponChild.Name
							
						end



						updateLoadout()
						
					end)
				end
			end)	
		end
	end)
end

assigning weapons (server-side):

local rs = game.ReplicatedStorage

local re = game.ReplicatedStorage:WaitForChild("EquipWeaponRE")
local re2 = game.ReplicatedStorage:WaitForChild("DeployRE")



local primary
local secondary
local melee


--giving remote events
local GiveFolder = game.ReplicatedStorage.WeaponGivingHandlers

local giveprimary = GiveFolder.GivePrimary
local givesecondary = GiveFolder.GiveSecondary
local givemelee = GiveFolder.GiveMelee

re.OnServerEvent:Connect(function(plr, gun)
	local weapons = plr.Weapons
	if gun and gun.Parent.Parent.Parent == weapons then

		local gunType = gun.Parent.Name

		if gunType == "Primary" then
			giveprimary:FireClient(plr, gun)
			primary = gun
			
		end
		if gunType == "Secondary" then
			givesecondary:FireClient(plr, gun)
			secondary = gun
			
		end
		if gunType == "Melee" then
			givemelee:FireClient(plr, gun)
			melee = gun
			
		end
	end
end)



rs.WeaponGivingHandlers.ToServer.ToServerPrimary.OnServerEvent:Connect(function(plr, gun)
	gun:Clone().Parent = plr.Backpack
end)
rs.WeaponGivingHandlers.ToServer.ToServerSecondary.OnServerEvent:Connect(function(plr, gun)
	gun:Clone().Parent = plr.Backpack
end)
rs.WeaponGivingHandlers.ToServer.ToServerMelee.OnServerEvent:Connect(function(plr, gun)
	gun:Clone().Parent = plr.Backpack
end)

assigning weapons (client-side):

--weapon containers
local primary
local secondary
local melee




--Getting variables' value

rs.WeaponGivingHandlers.GivePrimary.OnClientEvent:Connect(function(gun) primary = gun end)
rs.WeaponGivingHandlers.GiveSecondary.OnClientEvent:Connect(function(gun) secondary = gun end)
rs.WeaponGivingHandlers.GiveMelee.OnClientEvent:Connect(function(gun) melee = gun end)



local Actionfolder = rs.WeaponGivingHandlers.ToServer

local toserverprimary = Actionfolder.ToServerPrimary
local toserversecondary = Actionfolder.ToServerSecondary
local toservermelee = Actionfolder.ToServerMelee



script.Parent.Play.Frame.Button.MouseButton1Click:Connect(function()
	
	
	if rs.Booleans.Intermission.Value == false then
		if primary then
			task.wait(1)
			toserverprimary:FireServer(primary)
			
		end

		if secondary then
			task.wait(1)
			toserversecondary:FireServer(secondary)
			
		end

		if melee then
			task.wait(1)
			toservermelee:FireServer(melee)
			
		end
	else
		print('did not run')
	end
end)

delete weapons so player won’t use them in lobby and he could freely change it

--deletes all weapons being in the Backpack when round ends----------------------
local DeleteWeaponsEvent = rs.RewardsEvents.ResetAllRoundTotal

DeleteWeaponsEvent.OnServerEvent:Connect(function(player)
	if player.Backpack:GetChildren() then
		for i, e in pairs(player.Backpack:GetChildren()) do
			e:Destroy()
		end
	end
end)

If you need more details, please let me know! I’ll be glad for any help provided! :hidere:

1 Like

Instead of deleting them when the clients go back to the lobby what if you made another folder under the Player and parent them there? then reparent them to the backpack when needed?

1 Like

I’ll try that, one moment. I hope that’ll work

I think it’s not the problem with that, because not all weapons don’t function.

I quickly read through the code, but it looks like you’re giving tools using the client (localscripts).

This is new…
Do you have any server scripts in the tools that you’re cloning? If so, I’m 99% sure they won’t work as they don’t exist on the server side.

As for working the first time and breaking the second, are you getting any errors?

General idea: Clone on client side = only you can see it. Server can’t see something created by a localscript.

2 Likes

It is being cloned by server and nothing else

Now I see, oops.
I was trying to see where the remotes were going and got lost lol.

Are you getting any errors and can you provide screenshots of your issue?

I have class in a minute so hopefully someone else can solve this within the next hour.

2 Likes

If there would be an issue, I’d be guessing it’s here since it only runs once play button is pressed.
I don’t know how your game works, though, so I can’t assume much.

2 Likes

Errors occur only there:

re.OnServerEvent:Connect(function(plr, gun)
	local weapons = plr.Weapons
	if gun and gun.Parent.Parent.Parent == weapons then

		local gunType = gun.Parent.Name

		if gunType == "Primary" then
			giveprimary:FireClient(plr, gun)
			primary = gun
			
		end
		if gunType == "Secondary" then
			givesecondary:FireClient(plr, gun)
			secondary = gun
			
		end
		if gunType == "Melee" then
			givemelee:FireClient(plr, gun)
			melee = gun
			
		end
	end
end)

rs.WeaponGivingHandlers.ToServer.ToServerPrimary.OnServerEvent:Connect(function(plr, gun)
	gun:Clone().Parent = plr.Backpack
end)
rs.WeaponGivingHandlers.ToServer.ToServerSecondary.OnServerEvent:Connect(function(plr, gun)
	gun:Clone().Parent = plr.Backpack
end)
rs.WeaponGivingHandlers.ToServer.ToServerMelee.OnServerEvent:Connect(function(plr, gun)
	gun:Clone().Parent = plr.Backpack
end)

errors:
ServerScriptService.PlayDebug:42: attempt to index nil with ‘Parent’
ServerScriptService.PlayDebug:67: attempt to index nil with ‘Clone’
ServerScriptService.PlayDebug:70: attempt to index nil with ‘Clone’

1 Like

Try debugging with print(gun.Parent) and other prints the line before the error so you can locate the source.

Edit: I have another class now, be back in one hour.

2 Likes

I’m sorry for the late response, but I think it’s just because of a bad system I made. Thank you for help anyway!

3 Likes