Yeah, that’s true. Just its a good practice to use Json if you plan on per-suing a Programming/Computer Science career as so many platforms, languages, and development kits take use of it. Json is also good if you plan to link your DataStores with HttpService, which is nice as well.
I made this code, It’s what I’ve come up with so far. But it doesn’t seem to work.
What I mean with it now working is that it only does one of the addWeapon / addArmor functions instead of both.
-- Made by NeurionG / CraftDualMine
local ItemsFolder = game:GetService("ServerStorage"):WaitForChild("Items")
local WeaponFolder = ItemsFolder:WaitForChild("Weapons")
local httpService = game:GetService("HttpService")
local ArmorFolder = ItemsFolder:WaitForChild("Armor") -- START
local module = {}
local allItems = {
['1'] = {
Name = "Wooden Sword",
ID = "1",
Type = "Weapon",
Damage = 1,
AttackSpeed = 2.5,
Tradeable = false,
Rarity = "Common",
Location = WeaponFolder:FindFirstChild("Wooden Sword")
},
['2'] = {
Name = "Leather Armor",
ID = "2",
Type = "Armor",
Health = 10,
Tradeable = false,
Rarity = "Common",
Shirt = "rbxassetid://1180818752",
Pants = "rbxassetid://1180819059",
Location = ArmorFolder:FindFirstChild("Leather Armor")
}
}
local reverseItems = {
['Wooden Sword'] = "1",
['Leather Armor'] = "2",
}
local Enchantments = {
['Sharpness'] = true,
['Defense'] = true,
}
module.player = {}
function module.insertPlayerItem(plr, itemID, lvl, ench)
local converted = allItems[itemID]
if(converted) then
local unique = httpService:GenerateGUID(false)
if not(module.player[plr.UserId].Items[converted.ID]) then
table.insert(module.player[plr.UserId].Items, converted.ID)
else
warn("Player Already Had Data")
end
module.player[plr.UserId].Items = {
[unique] = {
ID = itemID,
Name = converted.Name,
Level = lvl,
Enchantments = ench,
UniqueID = unique
}
}
else
warn("itemID:", itemID, "was not found!")
end
end
function module.getUniqueByInfo(plr, itemID, lvl, ench)
local Items = module.player[plr.UserId].Items
for i,v in pairs(Items) do
if(v.ID == itemID and v.Level == lvl and v.Enchantments == ench) then
print(v.UniqueID, v.ID)
return v.UniqueID
end
end
end
function module.getIDByUnique(plr, UniqueID)
return module.player[plr.UserID].Items[UniqueID].ID
end
function module.updatePlayerItem(plr, itemID, lvl, ench)
local converted = allItems[itemID]
if(converted) then
local location = module.player[plr.UserId].Items[itemID]
if(location) then
location.Level = lvl
location.Enchantments = ench
end
end
end
function module.setNormalClothes(plr, shirtID, pantsID)
table.insert(module.player[plr.UserId], {
ShirtID = shirtID,
PantsID = pantsID,
})
print(module.player[plr.UserId].ShirtID, module.player[plr.UserId].PantsID)
end
function module.getNormalClothes(plr)
local tableNew = {}
local playerTable = module.player[plr.UserId]
table.insert(tableNew, playerTable.ShirtID)
table.insert(tableNew, playerTable.PantsID)
return tableNew
end
function module.getEquippedArmorID(plr)
if(plr.Character) then
for i,v in pairs(plr.Character:GetChildren()) do
if(v.ClassName == "Shirt") then
return reverseItems[v.Name]
end
end
return "2"
else
warn("Character was not found")
end
end
function module.getEquippedWeaponID(plr)
if(plr.Character) then
for i,v in pairs(plr.Backpack:GetChildren()) do
if(v.ClassName == "Tool") then
return reverseItems[v.Name]
end
end
return "1"
else
warn("Character was not found")
end
end
function module.returnAllItems()
return allItems
end
function module.stringToID(id)
return reverseItems[id]
end
local function addToInventory(plr, itemID, lvl, ench)
local Itemfound = allItems[itemID]
if(Itemfound.Name) then
if(Itemfound.Type == "Weapon") then
module.addWeapon(plr, itemID, lvl, ench)
elseif(Itemfound.Type == "Armor") then
module.addArmor(plr, itemID, lvl, ench)
else
warn("Type "..allItems[itemID].Type.." was not found")
end
else
warn(Itemfound, "is no registered item")
end
end
function module.maxExp(level)
return level.Value * 25
end
function module.Ranks(RankName)
local Ranks = {
['Helpless'] = Color3.fromRGB(233, 233, 233),
['Novice'] = Color3.fromRGB(212, 212, 212),
['Advanced Beginner'] = Color3.fromRGB(91, 212, 57),
['Competent'] = Color3.fromRGB(50, 212, 188),
['Proficient'] = Color3.fromRGB(44, 120, 212),
['Expert'] = Color3.fromRGB(57, 45, 212),
['Master'] = Color3.fromRGB(212, 40, 86),
['Dangerous'] = Color3.fromRGB(212, 142, 0),
['Deadly'] = Color3.fromRGB(212, 5, 8),
['Unstoppable'] = Color3.fromRGB(152, 0, 212),
['Legend'] = Color3.fromRGB(255, 187, 26),
['Godly'] = Color3.fromRGB(220, 0, 3),
-- Nongettable ranks
['AlphaTester'] = Color3.fromRGB(28, 220, 208),
['BetaTester'] = Color3.fromRGB(27, 114, 220),
['Trusted'] = Color3.fromRGB(184, 5, 220),
}
return Ranks[RankName]
end
function module.Permissions(player)
local Permissions = {
[87069273] = 'Owner',
['suppervipmanlol'] = 'Owner',
['Foxythepiratefo477'] = 'Visitor',
['UncontrollableVex'] = 'Visitor',
['PeterGriffinMyLove'] = 'Visitor'
}
return Permissions[player]
end
function module.playerItems(player)
local Inventory = player:WaitForChild("Inventory")
local playerItems = {}
for i,v in pairs(module.player[player.Name].Items) do
end
return playerItems
end
function module.addPlayerItem(player, itemID, ID, lvl, ench)
if not(allItems[itemID] or reverseItems[itemID]) then warn("itemID:", itemID, "does not exist in the database!") return end
if not ID then itemID = module.stringToID(itemID) end
addToInventory(player, itemID, lvl, ench)
end
function module.removePlayerItem(plr, itemID, ID)
local Stats = plr:WaitForChild("Stats")
local Inventory = plr:WaitForChild("Inventory")
local LastEquipped = Stats:WaitForChild("LastEquipped")
if not(allItems[itemID] or reverseItems[itemID]) then warn("itemID:", itemID, "does not exist in the database!") return end
if not ID then itemID = module.stringToID(itemID) end
local itemConverted = allItems[itemID]
if(itemConverted) then
if(itemConverted.Type == "Weapon") then
if(Inventory:WaitForChild("Weapons"):FindFirstChild(itemConverted.Name)) then
module.removeWeapon(plr, itemID)
end
elseif(itemConverted.Type == "Armor") then
if(Inventory:WaitForChild("Armor"):FindFirstChild(itemConverted.Name)) then
module.removeArmor(plr, itemID)
end
end
end
LastEquipped.Value = module.getLastEquipped(plr)
end
function module.startup(plr)
print("start")
if(module.player[plr.UserId]) then
print("Found Player")
local Items = module.player[plr.UserId].Items
print(Items)
for i,v in pairs(Items) do
local foundItem = allItems[v.ID]
print(foundItem)
if(foundItem.Type == "Weapon") then
module.addWeapon(plr, v.ID, v.Level, v.Enchantments)
elseif(foundItem.Type == "Armor") then
module.addArmor(plr, v.ID, v.Level, v.Enchantments)
end
end
end
end
function module.addWeapon(plr, ID, lvl, ench)
print(plr, ID, lvl, ench)
local WeaponTable = allItems[ID]
local stringed = allItems[ID].Name
for i,v in pairs(WeaponFolder:GetChildren()) do
if(v.Name == stringed) then
print("Adding weapon")
local UniqueID = module.getUniqueByInfo(plr, ID, lvl, ench)
print(plr, ID, lvl, ench)
local Clone = v:Clone()
Clone.Parent = plr:WaitForChild("Inventory"):WaitForChild("Weapons")
module.insertPlayerItem(plr, ID, lvl, ench)
Clone.Name = UniqueID
module.equipWeapon(plr, UniqueID)
end
end
end
function module.addArmor(plr, ID, lvl, ench)
local ArmorTable = allItems[ID]
local stringed = allItems[ID].Name
for i,v in pairs(ArmorFolder:GetChildren()) do
if(v.Name == stringed) then
print("Adding Armor")
local UniqueID = module.getUniqueByInfo(plr, ID, lvl, ench)
local Clone = v:Clone()
Clone.Parent = plr:WaitForChild("Inventory"):WaitForChild("Armor")
module.insertPlayerItem(plr, ID, lvl, ench)
print(UniqueID)
Clone.Name = UniqueID
module.equipArmor(plr, UniqueID)
end
end
end
function module.equipWeapon(plr, UniqueID)
local Inventory = plr:WaitForChild("Inventory")
local Weapons = Inventory:WaitForChild("Weapons")
local foundItem = Weapons:FindFirstChild(UniqueID)
if(foundItem) then
if not(plr.Backpack:FindFirstChild(UniqueID)) then
local Clone = foundItem:Clone()
Clone.Name = foundItem.Name
Clone.Parent = plr.Backpack
else
warn("Player already had equipped that weapon")
end
else
warn("The server was trying to equip the ID", UniqueID, "to", plr.Name.."'s inventory without it being in the person's inventory!")
end
end
function module.equipArmor(plr, UniqueID)
local Inventory = plr:WaitForChild("Inventory")
local Armor = Inventory:WaitForChild("Armor")
local lastEquipped = plr:WaitForChild("Stats"):WaitForChild("LastEquipped")
local foundItem = Armor:FindFirstChild(UniqueID)
if(foundItem) then
for i,v in pairs(plr.Character:GetChildren()) do
if(v.ClassName == "Shirt" or v.ClassName == "Pants") then
v:Destroy()
end
end
for i,v in pairs(foundItem:GetChildren()) do
local clone = v:Clone()
clone.Parent = plr.Character
end
lastEquipped.Value = module.getLastEquipped(plr)
else
warn("The server was trying to equip the ID", UniqueID , "to", plr.Name.."'s inventory without it being in the person's inventory!")
end
end
function module.removeWeapon(plr, ID)
local Inventory = plr:WaitForChild("Inventory")
local Weapons = Inventory:WaitForChild("Weapons")
local lastEquipped = plr:WaitForChild("Stats"):WaitForChild("LastEquipped")
local stringed = allItems[ID].Name
for i,v in pairs(Weapons:GetChildren()) do
if(v.Name == stringed) then
module.unEquipWeapon(plr, ID)
v:Destroy()
lastEquipped.Value = module.getLastEquipped(plr)
end
end
end
function module.unEquipWeapon(plr ,ID)
local plrWeapon = plr.Backpack
local stringed = allItems[ID].Name
local directory = plrWeapon:FindFirstChild(stringed)
if(directory) then
directory:Destroy()
end
end
function module.removeArmor(plr, ID)
local Inventory = plr:WaitForChild("Inventory")
local Armor = Inventory:WaitForChild("Armor")
local lastEquipped = plr:WaitForChild("Stats"):WaitForChild("LastEquipped")
local stringed = allItems[ID].Name
for i,v in pairs(Armor:GetChildren()) do
if(v.Name == stringed) then
module.unEquipArmor(plr, ID)
v:Destroy()
lastEquipped.Value = module.getLastEquipped(plr)
end
end
end
function module.unEquipArmor(plr, ID)
local plrArmor = plr.Character
local stringed = allItems[ID].Name
local directory = plrArmor:FindFirstChild(stringed)
if(directory) then
directory:Destroy()
end
end
function module.getLastEquipped(plr)
local lastArmorID = module.getEquippedArmorID(plr)
local lastWeaponID = module.getEquippedWeaponID(plr)
local newLastEquippedValue = {}
table.insert(newLastEquippedValue, lastArmorID)
table.insert(newLastEquippedValue, lastWeaponID)
return table.concat(newLastEquippedValue, ", ")
end
function module.getLastEquippedTable(plr)
local lastArmorID = module.getEquippedArmorID(plr)
local lastWeaponID = module.getEquippedWeaponID(plr)
local newLastEquippedValue = {}
table.insert(newLastEquippedValue, lastArmorID)
table.insert(newLastEquippedValue, lastWeaponID)
return newLastEquippedValue
end
function module.getAllNonEquippedWeapons(plr)
local EquippedWeapon = reverseItems[module.getEquippedWeaponID(plr)]
local PlayerItems = module.playerItems(plr)
local OtherWeapons = {}
for i,v in pairs(PlayerItems) do
if not(v == EquippedWeapon) then
table.insert(OtherWeapons, v)
end
end
return OtherWeapons
end
function module.unEquipAll(plr)
if(plr.Character) then
for i,v in pairs(plr.Character:GetChildren()) do
if(v.ClassName == "Shirt" or v.ClassName == "Pants") then
v:Destroy()
end
end
for i,v in pairs(ArmorFolder:WaitForChild("2")) do
local Clone = v:Clone()
Clone.Parent = plr.Character
end
end
end
function module.equipLastEquipped(plr)
local lastEquippedTable = module.getLastEquippedTable(plr)
for i,v in pairs(lastEquippedTable) do
local foundItem = allItems[v]
if(foundItem.Type == "Weapon") then
module.equipWeapon(plr, foundItem.ID)
elseif(foundItem.Type == "Armor") then
module.equipArmor(plr, foundItem.ID)
end
end
end
return module
I’m guessing it does one because you do this
if(foundItem.Type == "Weapon") then
-- addweapon function
elseif(foundItem.Type == "Armor") then
-- addarmor
end
So if you have weapon it’ll add weapon but skip over the armor part.
Thanks, but that was not the issue, as my may see there’s a for loop over that loops through all the items.
The issue was that I did like this:
module.player[plr.UserId].Items = {
[unique] = {
ID = itemID,
Name = converted.Name,
Level = lvl,
Enchantments = ench,
UniqueID = unique
}
}
Instead of
table.insert(module.player[plr.UserId].Items, {
[unique] = {
ID = itemID,
Name = converted.Name,
Level = lvl,
Enchantments = ench,
UniqueID = unique
}
})