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.
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.
Do note, If there’s a better way to do this, please let me know
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:
So far, it’s working. But when I switch loadouts, this is what happens.
the CurrentPlr returns to the (value-1) state, but the Loadout fires twice?
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.