What do you want to achieve?
Pet Inventory UI Text Status become to “UNEQUIP” upon player joins (data store equipped pets)
What is the issue?
For some example, player joined with 3 pets (data store equipped pets) 1 Bunny and 2 other is Dog. I wanna make the Pet UI text status became “UNEQUIP”. But the problem is
Calling them like that will always return the first child of that name. You’ll have to loop through all of the children. It would be best to send all pets at once in a list then you can do this
local function petsEquipped(petsList)
local pets = {} --I’m changing how the table stores the information. You could do this on the server instead.
for _, petName in pairs(petList) do
pets[petName] = (pets[petName] or 0) + 1 --Increment pet name if it exists, or create it if it doesn’t.
end
--Now pets[name] is equal to the amount of pets they have of that name.
for _, ui in pairs(PetUI.ScrollingFrame:GetChildren()) do
if pets[ui.Name] and pets[ui.Name] > 0 then
ui.EquipButton.Text = "UNEQUIP"
ui.EquipButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
pets[ui.Name]-=1
end
end
end
It is worth noting if you want to send them one at a time however you can do something similar, just loop through all the frames until you hit the first frame with the name of the pet that isn’t already set to “UNEQUIP”
Tried this method but doesn’t work.
here’s the updated one
game.ReplicatedStorage["Pets&EggFolder"].Remotes.PetsSelectedRE.OnClientEvent:Connect(function(PetName)
local petsCollected = {}
for _, child in pairs(PetsFolder:GetChildren()) do
petsCollected[child] = (petsCollected[child] or 0) + 1
end
PetModule:AddPet(PetName)
limitation.Value += 1
for _, ui in pairs(PetUI.ScrollingFrame:GetChildren()) do
if petsCollected[ui.Name] and petsCollected[ui.Name] > 0 then
ui.EquipButton.Text = "UNEQUIP"
ui.EquipButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
petsCollected[ui.Name] -= 1
end
end
game.ReplicatedStorage:WaitForChild("Pets&EggFolder"):WaitForChild("Remotes").ClickPlusRE:FireServer(PetName, true)
end)
Assuming you’re sending one pet at a time (sorry for flipping between data communication expectations again)
game.ReplicatedStorage["Pets&EggFolder"].Remotes.PetsSelectedRE.OnClientEvent:Connect(function(PetName)
PetModule:AddPet(PetName) -- Equip Pet
limitation.Value += 1 -- Adds up limitation value
game.ReplicatedStorage:WaitForChild("Pets&EggFolder"):WaitForChild("Remotes").ClickPlusRE:FireServer(PetName, true)
tablePets[#tablePets+1] = PetName
-- Below lines is the problem
local container = nil --Check to see if there is a child of PetUI.ScrollingFrame that hasn't been equipped yet.
for _, v in pairs(PetUI.ScrollingFrame:GetChildren()) do
if v.Name == PetName and v.EquipButton.Text ~= "UNEQUIP" then
container = v
break
end
end
if container then
container.EquipButton.Text = "UNEQUIP"
container.EquipButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
else
error("You don't have enough children of ScrollingFrame named " .. PetName .. " that aren't currently equipped")
end
end)
Hopefully that works
Usually when I’m dealing with inventory type stuff I actually assign each item an ID so that I can just tell the client what is happening to ID rather than by name which removes identical naming errors. That takes a bit more to setup though and isn’t ideal for every situation.
Break is actually really simple. Take any loop and if it hits the line break it will terminate the loop and move on to the next line
for i=1, 5 do
if i > 3 then
break
end
end
print("loop ended")
The code above would print 1 2 3 “loop ended”. It wouldn’t print 4 or 5 because when it hit 4 it hit the break statement which jumped the code past the loop.
So in my code
for _, v in pairs(PetUI.ScrollingFrame:GetChildren()) do
end
That loop is going to go through every child of ScrollingFrame. Once it finds a valid answer it sets container to that answer so the next part of the code can use it. The problem? Lets say we had 500,000 children in ScrollingFrame for some reason. Now it just so happens that the first child passes the if statement and is acceptable to be used in the next part of code. Instead of moving on like any human would, the computer just goes through every child in the list like you told it to despite having the data you’re looking for. Instead of potentially wasting that much computer power it’s best just to stop the loop once it has gotten an acceptable answer. There are other cases where going past the first acceptable answer can break a program, but this example instead of setting container equal to the first valid answer and moving on, the code would just check all of them and give you the last valid answer if the break wasn’t there. Which would work of course, but is a waste.
For another example
while true do
print("HI")
break
end
print("loop ended")
This will print “HI” “loop ended”. Not a practical example, but despite the fact that otherwise that loop would be infinite, the break will terminate it.
.
.
.
Another keyword for more loop control is ‘continue’. What continue means is, skip the rest of the code for this iteration and move to the next.
for i = 1, 5 do
if i == 3 then
continue
end
print(i)
end
print("loop ended")
Which would print: 1 2 4 5 “loop ended” since it hit the continue keyword on 3 which told it to go back to the top again at 4. Again my example is not the most practical, but a good example of exactly what it’s doing.
Ah I see what’s the point of putting “break” inside for i v loops. I might use this method on other stuffs and it’s making scripting more better. Very informative stuffs
I might wanna learn back how for i v loops works. Haven’t script for so long.
Thank you for helping me on this case. I’ve been thinking how can I do this. At least I got to learn something like how “break” works inside for i loops and “continue”