Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you listen to changes of multiple objects?

Asked by 4 years ago

So with one of my recent projects I've decided to work on a card game. Data of each specific card is stored in a folder pertaining to that card such as stats, name, text, etc. But one issue I've been thinking about is what to do in the situation of data manipulation, for example, buffing a unit. I've looked around but I haven't found any solutions whatsoever.

Will I have to setup a listener for each individual object, one for health, one for attack, etc, or is there some way I could listen to all of the Value objects within the folder at once to see if any of them has changed?

1
Not really sure what you are talking about but i think what you are looking for is a for loop. XviperIink 428 — 4y
0
No. A for loop is definitely not going to help. I'm trying to listen for changes in properties of several objects at once. For example, ObjectValue.Changed:Connect etc. But instead of say, just the one objectvalue, I could listen for five of them on the same listener. XxTrueDemonxX 362 — 4y
0
Aren't the values of cards static? Why, then, would you need to listen to changes? Wouldn't there be no changes? User#29813 0 — 4y
0
In the post above I specifically listed that there would be changes such as buffing a unit. If you're unfamiliar with the term, an example would be giving the card an extra 3 attack or health. XxTrueDemonxX 362 — 4y
View all comments (3 more)
0
That doesn't mean that you need to listen to changes... wouldn't you just access those values when you play the card? User#29813 0 — 4y
0
I would need to actually update the visual of the card itself for when said changes take place so I don't just randomly have something with 1 attack doing 4 damage because it's attack got buffed but wasn't shown on the card. XxTrueDemonxX 362 — 4y
0
In that case, I'd create a base card class and loop through every card to intialize it. So yes, a generic for loop is the way to go here (even if you don't take the OOP approach). User#29813 0 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

I've read the comments and based on what you said of listening for changes in multiple objects at once, a loop is definitely what you need.

So one of the normal harder solutions would just to be manually connecting all the events like so:

card.attack.Changed:Connect(function()

end)

card.defence.Changed:Connect(function()

end)

card.speed.Changed:Connect(function()

end)

-- and so on

for an easier way to do this is to iterate through using a loop!

for _, stat in pairs(card:GetChildren()) do
    stat.Changed:Connect(function()
        -- this will connect all the stats to a listener
    end)
end

and if want specific ones you can do:

for _, stat in pairs(card:GetChildren()) do
    if stat.Name ~= "name" and stat.Name ~= "Text" then -- name and text will not be connected
        stat.Changed:Connect(function()
            -- this will connect all the stats to a listener
        end)
    end
end

Now obviously this is a visual to help you understand what you can do so copy pasting this will not work.

PS: Remember not to connect the listeners to the cards in the storage rooms and make sure to connect them to the cards in game. This many be an common error for some people.

Good luck

EDIT: (Global Listener for workspace)

for _, instance in pairs(workspace:GetDescendants()) do
    if instance:IsA("IntValue") or instance:IsA("StringValue") or instance:IsA("NumberValue") then -- add on here of what you want to listen to
        instance.Changed:Connect(function()
            print("Test") -- make sure this script is server script in serverscriptservice
        end
    end
end

workspace.DescendantAdded:Connect(function(instance) -- whenever something is added to workspace
    if instance:IsA("IntValue") or instance:IsA("StringValue") or instance:IsA("NumberValue") then -- add on here of what you want to listen to
        instance.Changed:Connect(function()
            print("Test") -- make sure this script is server script in serverscriptservice
        end
    end
end)
0
Perhaps I'm not being quite accurate with my question or there's something particular that I'm missing with this for loop method. As a quick example, let's say when the card's health, attack, or mana value is ever changed, the server will print "Test". No matter what way I write or run a loop, I receive 0 output as if none of the listeners exist in the first place. XxTrueDemonxX 362 — 4y
0
@XxTrueDemonxX that's because you probably never change the value or never connect it to the correct values BlackOrange3343 2676 — 4y
0
@XxTrueDemonxX I can add a global listener in edits in workspace so anything inside workspace will be listened too, if you want listeners in the replicated storage or server storage which i don't know why you would have you can just change the script BlackOrange3343 2676 — 4y
0
Ah no. I found out what the issue was. I was changing the values directly within the explorer which does not trigger the changed event. I completely forgot about that. Apologies to viper for doubting him. I'm curious though, will these listeners disappear as well once the actual cards are removed? I'm not sure on all the specifics of how stuff like that works. XxTrueDemonxX 362 — 4y
View all comments (4 more)
0
Like, I'll have to keep reestablishing these for new copies of the cards. Will there ever be a point that it might cause performance issues because I have a bunch of loose ends floating about? XxTrueDemonxX 362 — 4y
0
Haha. No worries, now you have learnt that for loops can do this too xD.I Dont think that this simple for loop would cause performance issues unless you are talking about like 500 plus of this code. XviperIink 428 — 4y
0
@XxTrueDemonxX the events and listeners will automatically disconnect if the object associated with it is destroyed. Make sure you destroy those cards and you won't have to constantly establish the connections as the loop will do so. BlackOrange3343 2676 — 4y
0
@XxTrueDemonxX with the global listener code I provided you won't need any more code to look for changes in cards, it will detect any new cards added to workspace and will add it to the list of listeners BlackOrange3343 2676 — 4y
Ad

Answer this question