Add All Values not working

Hi everyone!

I have a problem with a certain script, I wanted to create a script that would work in such a way that it collects all NumberValue values ​​locally and adds them all to one. However, when the time is up, I want the value of it to be removed.

I created this script (but it doesn’t work, no error pops up either, it just doesn’t work):

local player = game.Players.LocalPlayer
local data = player:WaitForChild("Data")
local allStats = data:WaitForChild("AllStats")

while wait() do
	local potions = data:GetChildren()

	for i = 1,#potions do
		local PotionX2 = potions:GetChildren("PotionX2")
		
		allStats.Value = PotionX2
		
		print(allStats.Value)
	end
end

Screens how it looks:
png

I want it to collect “PotionsX2” (NumberValue) from each folder and add all of them to “AllStats”.
I’ve tried everything, but it was useless, so I am asking you for help.

You put data inside the players, not the player

It doesn’t matter, that’s not why the script doesn’t work.

potions:GetChildren(“PotionX2”) does not do what you think it does. i don’t even know what that does tbh because it’s not even a feature of the roblox api

try doing something like this instead

for i = 1, #potions do
	total += potions[i].PotionX2.Value
end
1 Like

I’m sorry if I’m bothering you

another

This arrangement must be in order to conform to this code

local player = game.Players.LocalPlayer
local data = player:WaitForChild(“Data”)
local allStats = data:WaitForChild(“AllStats”)

Error:
PotionX2 is not a valid member of NumberValue “Players.adaspo.Data.AllStats”

To make it work you’ll need to replace this line:

local potions = data:GetChildren()

with this line:

local potions = data:GetDescendants()

:GetChildren() only returns the immediate children whereas :GetDescendants() will grab every object.

This should work, but it is in an infinite loop. Meaning this will constantly add to AllStats …forever.

local replicatedStorage = game:GetService("ReplicatedStorage")
local data = replicatedStorage:WaitForChild("Data")
local allStats = data:WaitForChild("AllStats")

while true do --it's better to do it like this so you don't needlessly wait that first time
	local potions = data:GetDescendants()

	for i, v in pairs(potions) do
		if v.Name == "PotionX2" then
			allStats.Value += v.Value
			print(allStats.Value)
		end
	end
	wait()
end

If that isn’t what you want, then just remove the while true do (and the “end”) and it’ll only run once.

A couple of sidenotes though: I suggest reading through this link so you know more about what is actually happening when you call wait() Avoiding wait() and why.

Also, a lot of scripters start out using value instances, but instead switch to storing the values in variables/tables. There are several reasons for doing this, but just know that every time you change that value instance from the server in a space that’s viewable by both the client and the server …there is a built-in remote that is fired to the client. This means it can be expensive bandwidth wise if you change too many large values too many times.

It is now working properly

I made the code a bit more functional/practical since I wasn’t happy with my previous example. This one is a bit faster to run and more flexible since it can be activate when needed:

local replicatedStorage = game:GetService("ReplicatedStorage")
local data = replicatedStorage:WaitForChild("Data")
local allStats = data:WaitForChild("AllStats")

--Only do it once when needed:
local function updateAllStats()
	local potions = data:GetChildren() --Made it more efficient by using :GetChildren()

	for i, v in pairs(potions) do
		--Since you know what the name is called you can refer to it directly and skip grabbing all the descendants
		if v.Name ~= "AllStats" then --if it isn't AllStats
			allStats.Value += data[v.Name].PotionX2.Value -- Point directly to it
			print(allStats.Value)
		end

	end
end

updateAllStats() --Put this anywhere else in this script when you want to update AllStats

This code will error if you add anything else as a direct child of Data, or if you rename/remove any of the PotionX2’s.

That’s right for me, it almost works, but it only gets me value from one PotionX2, more specifically from “Potion12h”, but when I removed it, it got value from another Potion1D, but didn’t add value from Potion2D

Img:

i do not speak English

Be patient with me in order to reach what you want

You want it to be like this, for example

allStats.Value = Potion1D.PotionX2.Value + Potion12h.PotionX2.Value + Potion2D. PotionX2.Value

local player = game.Players.LocalPlayer
local data = player:WaitForChild("Data")
local allStats = data:WaitForChild("AllStats")


local potions = data:GetDescendants()

for i = 1,#potions do
    --print(potions[i])
	if potions[i].Name == "PotionX2" then
	   --print(potions[i])
	   allStats.Value = allStats.Value + potions[i].Value 
	   print(allStats.Value)
	end
end

It only loads once, but it needs an event to function properly

This is what I mean by the event, it works correctly

But you had an error in the location of the children and grandchildren

stilll dosen’t works ;/
I need it in while wait() do

You must specify to me exactly when you want him to collect the values

If you want him to collect when launching an event or to collect continuously

I want to colllect it always :open_mouth:

And how you whistle the meter, you will have big numbers

I will add to infinity

local player = game.Players.LocalPlayer
local data = player:WaitForChild("Data")
local allStats = data:WaitForChild("AllStats")

while true do
wait()
local potions = data:GetDescendants()

for i = 1,#potions do
    --print(potions[i])
	if potions[i].Name == "PotionX2" then
	   --print(potions[i])
	   allStats.Value = allStats.Value + potions[i].Value 
	   print(allStats.Value)
	end
end
end

This is an example of an function and it would be an example of the mouse

Adds all the numbers when pressed Button1Down

When Button1Up is, AllStats is zero

Adds numbers again when Button1Down is pressed again

This code when placed inside a LocalScript in StarterPlayer in StarterCharacterScripts

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local data = player:WaitForChild("Data")
local allStats = data:WaitForChild("AllStats")

local function updateAllStats()
local potions = data:GetDescendants()

for i = 1,#potions do
    --print(potions[i])
	if potions[i].Name == "PotionX2" then
	 --print(potions[i])
	   allStats.Value = allStats.Value + potions[i].Value
	end			
end
print("allStats.Value   ", allStats.Value)	
end

local function resetAllStats()	
	  allStats.Value = 0
	  print("allStats.Value   ", allStats.Value)
end

Mouse.Button1Down:Connect(function()
     --print("Button 1 is down")
	 updateAllStats()
end)

Mouse.Button1Up:Connect(function()
    --print("Button 1 is Up")
   resetAllStats() 
end)

Tell me if something is not working properly

I wish you good luck in what you do