How would I fix this loadout issue?

Greetings! I have a loadout system I made, which I think is a bit unique in ways, but with uniqueness comes with difficulty, and that’s where I’m stuck today.

What is the problem?

To understand the problem, I’ll explain the system briefly. when the player joins, the server creates an attribute called “Loadout”, which is set to “None” at the start.

image

When the player clicks a loadout, the loadout attribute is set to the designated loadout name and it sends the client the number of players that have selected the loadout.

image

Do note, If there’s a better way to do this, please let me know :skull:

The module script contains all the relevant information about each classes. I’m not too experienced with modules and it’s my first time using them. The “CurrentPlr” refers to the amount of players currently in the team and “PlrCount” means the max capacity for that loadout. LName is just the display loadout name.

Main Error:

When I press a loadout, for example, a loadout called “Recon”, this is what happens:

image

So far, it’s working. But when I switch loadouts, this is what happens.

image

the CurrentPlr returns to the (value-1) state, but the Loadout fires twice?

image

The loadout attribute does change, so this is a bit confusing.

Once again, I cannot express how grateful I am for this help, it means a lot. However, I’d like to clarify I am not looking for entire code, rather just a guide, help or resource towards fixing the error.

Without further waiting, here’s the bit of code:

Server

replicatedstorage.LoadoutSelect.OnServerEvent:Connect(function(player, loadout, team)
	
	local char = player.Character
	
	local player_loadout = player:GetAttribute("Loadout")
	
	if player_loadout == "None" then
		
		print("NONE FIRE")
		
		-- plr's attribute loadout is none
		
		player:SetAttribute("Loadout", loadout.LName)
		
		local player_loadout = player:GetAttribute("Loadout")
		
		local returned_loadout = loadoutmodule.returnClass(player_loadout)
		
		loadoutmodule.updatePlr(returned_loadout, true)
		
		replicatedstorage.UpdateLoadout:FireAllClients(returned_loadout, true)
		
	elseif player_loadout ~= "None" then
		
		-- plr's attribute loadout is not none
		
		local player_loadout = player:GetAttribute("Loadout")
		
		print("BEFORE:"..player_loadout)
		
		--player:SetAttribute("Loadout", loadout.LName)
		
		local returned_loadout = loadoutmodule.returnClass(player_loadout)
		
		loadoutmodule.updatePlr(returned_loadout, false)
		
		replicatedstorage.UpdateLoadout:FireAllClients(returned_loadout, false)
		
		player:SetAttribute("Loadout", loadout.LName)
		
		replicatedstorage.UpdateLoadout:FireAllClients(returned_loadout, true)
		
		loadoutmodule.updatePlr(returned_loadout, true)
		
	end
end)

Client

function onUpdateLoadout(returnedloadout, value)
	
	print(returnedloadout, tostring(value))
	
	for index, ui in pairs(scrollframe:GetChildren()) do
		if ui:IsA("GuiObject") then
			if ui.Name == returnedloadout.LName then
				-- same thing
				
				ui.PlayerCount.Text = tostring(returnedloadout.CurrentPlr).."/"..tostring(returnedloadout.PlrCount)
			end
		end
		
	end
	
end

ModuleScript (all)

local players = game:GetService("Players")

local serverstorage = game:GetService("ServerStorage")

local classes = {
	
	--// LOADOUT FOR A TEAM
	
	["Red"] = {
		
		
		
		
		["Rifleman"] = {
			
			["LName"] = "Rifleman"; -- name of loadout
			
			["PlrCount"] = 18; -- max player limit
			
			["CurrentPlr"] = 0; -- current player count
			
		};
		
		
		

		["Recon"] = {
			
			["LName"] = "Recon";
			
			["PlrCount"] = 9;
			
			["CurrentPlr"] = 0;
			
		}
	};
	
	--// LOADOUT FOR B TEAM
	
	["Blue"] = {
		
		
		
		
		["Assault"] = {
			
			["LName"] = "Assault";
			
			["PlrCount"] = 18;
			
			["CurrentPlr"] = 0;
			
		};
		
		
		
		
		
		["Sniper"] = {
			
			["LName"] = "Sniper";
			
			["PlrCount"] = 9;
			
			["CurrentPlr"] = 0;
			
		}
	}
	
}

function classes.createLoadoutFolder(loadout, team)
	
	local loadoutfolder = Instance.new("Folder")
	loadoutfolder.Name = loadout.Name
	loadoutfolder.Parent = serverstorage.Tools[team.Name]
	
	return loadoutfolder
	
end

function classes.updatePlr(loadout, value)
	if value == true then
		-- positive player
		loadout.CurrentPlr += 1
	elseif value == false then
		-- negative player
		loadout.CurrentPlr -= 1
	end
end



function classes.returnClass(name)

	for index, team in pairs(classes) do
		if typeof(team) == "function" then continue end
		for key, value in pairs(team) do
			if key == name then
				-- found
				return value
			end
		end
	end

end

return classes

ModuleScript (possibly main error)

function classes.updatePlr(loadout, value)
	if value == true then
		-- positive player
		loadout.CurrentPlr += 1
	elseif value == false then
		-- negative player
		loadout.CurrentPlr -= 1
	end
end



function classes.returnClass(name)

	for index, team in pairs(classes) do
		if typeof(team) == "function" then continue end
		for key, value in pairs(team) do
			if key == name then
				-- found
				return value
			end
		end
	end

end

I made some print statements for checking things, and they seem to be working. I also added comments so you may understand what’s going on.

1 Like

I started reading this. So far what i can see my self i started getting more into module scripts.

Could the problem be that in the first server script both of the if statmemnets are set to None? Shouldn’t they be Red and Blue?

1 Like

I will try to replicate the issue soon. Then i could maybe could find the problem.

Red and Blue are the teams for each loadout. The player’s attribute only saves their loadout, I don’t really think the teams are needed.

When you put your print statements did you just put a print, or did you print things like variables?

For example if you have:

if not x == true then
    print("false")
    -- do your false code
else
    -- do your true code
end

But if x == nil then your false code will run as well.

If you put print(x) just before your if statement it can help you troubleshoot.

So, when the player’s loadout is set to “None” upon joining, this is what it says:
image

and then it changes to the selected loadout.

image

image

here’s what happens after selecting another loadout.

BEFORE: refers the one that is before being selected, being the sniper loadout of course, then it sets the sniper currentplr to 0 and fires again.

1 Like

So im either not smart enough to qualify as a programmer or this could be the error.

image
Didn’t you make it so its true and false?

The events take 2 params, the loadout itself (the module class) and the boolean value. if the value is false, then it makes the currentplr - 1. If the value is true, then it makes the currentplr + 1. and the player attribute just sets the loadout again so it makes the new loadout + 1. It’s hard to understand, I know.

Yeah but then why you call it “Yes its false” and later “Yes true”
image

And this piece of code is in one of the if statements so it prints true and false.
Or am i really off it. This could be the reason why its true at first and then its false?

I don’t understand how do you call the client function there is no remote or mouse buttonclick?

In the code what you provided.

The code given is just a small chunk of it, but yes it does fire using a mousebutton1click function in the client.

Okay just wanted to make sure :wink:

@Panz0idz What do you think about this? Could this possibly be the issue?

(question 1)

The first time it’s false is when the script gets the player’s current loadout value. using the returnClass module, I found which loadout it is given with the loadout name. With that loadout found, I use the updatePlr() function and send in the loadout as well with the value of false (false will make the currentplr value to v-1). Then the UpdateLoadout fires to client sending the returned loadout and false. We set the loadout, and repeat the process with the true one (question 2)

I think you are teaching me more than I am you… :thinking:

I’ve read a post where they say that ModuleScripts don’t replicate, but rather stay in respective server and client. Part of me thinks that it didn’t update when sending to the client?

1 Like

I just found out that i copied the server code twice.

I’m not that good, but I do see your point. I myself am a bit confused at what I wrote, and I don’t this is the best way to do it, but it sort of works if this problem might be fixed.

Ok I just realized a massive breakthrough, this could be the potential fix, I will be testing and responding soon.

1 Like

To be honest im not the best and I am only learning.

At these situations NEVER give up! There is always a solution. So if i was you i would place these scripts in a folder, disable them and put the folder in the Server storage and start from nearly scratch. I use this method when im creating something new just to test it and if i would need to get the old scripts back i could. So yeah I think you could try that. Try a different method maybe somebody smarter than two of us will find this post and will find this issue.