Is there a way to shorten this code, maybe with a for loop or something?
The code (a LocalScript placed in StarterPlayerScripts):
local LocalPlayer = game:GetService("Players").LocalPlayer
local inv = LocalPlayer:WaitForChild("Inventory")
local rs = game:GetService("ReplicatedStorage")
local invEvent = rs:WaitForChild("InvAdd")
local Weapons = inv:WaitForChild("Weapons")
local Shields = inv:WaitForChild("Shields")
local Clothes = inv:WaitForChild("Clothes")
local Materials = inv:WaitForChild("Materials")
local Food = inv:WaitForChild("Food")
local Keys = inv:WaitForChild("Keys")
local function childAddWeapons()
local cat = "Weapons"
invEvent:FireServer(cat)
end
local function childAddShields()
local cat = "Shields"
invEvent:FireServer(cat)
end
local function childAddClothes()
local cat = "Clothes"
invEvent:FireServer(cat)
end
local function childAddMaterials()
local cat = "Materials"
invEvent:FireServer(cat)
end
local function childAddFood()
local cat = "Food"
invEvent:FireServer(cat)
end
local function childAddKeys()
local cat = "Keys"
invEvent:FireServer(cat)
end
Weapons.ChildAdded:Connect(childAddWeapons)
Shields.ChildAdded:Connect(childAddShields)
Clothes.ChildAdded:Connect(childAddClothes)
Materials.ChildAdded:Connect(childAddMaterials)
Food.ChildAdded:Connect(childAddFood)
Keys.ChildAdded:Connect(childAddKeys)
This is all based around a series of strings. You can put the strings for your categories into an array and loop through it to get the category instance and make the ChildAdded connection. This would collapse everything from line 5 downwards.
The security of what you’re doing sounds questionable though, just a thought. Hard to say.
local LocalPlayer = game:GetService("Players").LocalPlayer
local inv = LocalPlayer:WaitForChild("Inventory")
local rs = game:GetService("ReplicatedStorage")
local invEvent = rs:WaitForChild("InvAdd")
local Spots = {
["Weapons"] = inv:WaitForChild("Weapons"),
["Shields"] = inv:WaitForChild("Shields"),
["Clothes"] = inv:WaitForChild("Clothes"),
["Materials"] - inv:WaitForChild("Materials"),
["Food"] = inv:WaitForChild("Food"),
["Keys"] = inv:WaitForChild("Keys")
}
local function FireThing(Cat)
invEvent:FireServer(Cat)
end
for i,v in pairs(Spots) do
v.ChildAdded:Connect(function()
FireThing(i)
end)
end
I noticed alot of the repetition, and the signal being transfers is the same as the parent of the child added, so I came up with this,
function FireCategoryAdded(...)
local Parents={...}
for Index,Parent in ipairs(Parents) do
Parent.ChildAdded:Connect(function(child)
invEvent:FireServer(Parent.Name)
end))
end
end
and then you can just plugin like FireCategoryAdded(Weapons,Sheilds,Cloths)
Since you are firing the name back this can be quite short if we search by the same string we fire.
It looks like this script should be on the server side, unless you are explicitly generating content in local scripts (and thus allowing more power to exploiters) the server can use the same .ChildAdded connections better.
local LocalPlayer = game:GetService("Players").LocalPlayer
local inv = LocalPlayer:WaitForChild("Inventory")
local rs = game:GetService("ReplicatedStorage")
local invEvent = rs:WaitForChild("InvAdd")
local categories = {
"Weapons", "Shields", "Clothes", "Materials", "Food", "Keys"
}
for _, category in ipairs(categories) do
local InvCategory = inv:WaitForChild(category)
InvCategory.ChildAdded:Connect(function()
invEvent:FireServer(category)
end)
end