Hey!
After writing this section of code for my spell creation system, I was curious if there was any way to further optimize this?
-- Static Values: CheckSpell = ["SpellName1"]
game.ReplicatedStorage.Storage.CraftSpell.OnServerInvoke = function(player, spellName)
local playerData = game.ServerStorage.PlayerData:FindFirstChild(player.Name)
if not playerData then return end
if SpellRequirements[spellName] == nil then return end -- player tried to request a non existent spell
print(spellName)
local CheckSpell = game.HttpService:JSONDecode(playerData.SpellsCreated.Value)
if table.find(CheckSpell, spellName) ~= nil then return false end -- player tried to craft the same spell twice
local GetItems = string.split(SpellRequirements[spellName].ScriptCost, ",")
local VerifyItems = {}
-- get all items and check if they match the ones in the backpack
for i,v in pairs(GetItems) do
for o,b in pairs(player.Backpack:GetChildren()) do
print(v)
if v == b.Name then
table.insert(VerifyItems, b.Name)
end
end
end
local JSONVerifyItems = game.HttpService:JSONEncode(VerifyItems)
-- if player meets the requirement then we give them the spell
if JSONVerifyItems == SpellRequirements[spellName].Requirements then
local createdSpells = game.HttpService:JSONDecode(playerData.SpellsCreated.Value)
table.insert(createdSpells, spellName)
playerData.SpellsCreated.Value = game.HttpService:JSONEncode(createdSpells)
print("Found all items")
for i,v in pairs(VerifyItems) do
player.Backpack:FindFirstChild(v):Destroy() -- destroy their items
end
return true
else
return false
end
end