Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Value change based on GetChildren?

Hey, I'm very new to scripting and wondering what im missing/doing wrong here to try and get the result I'm looking for.

When a Part gets added to a Folder i want it to update as a Value of how many Objects are in that Folder.

Local Script:

local cookiecounter = game.ReplicatedStorage:WaitForChild("CookieCount")

script.Parent.Text = cookiecounter.Value
cookiecounter.Changed:Connect(function()
script.Parent.Text = cookiecounter.Value
end)

Script:

local cookiecounter = game.ReplicatedStorage:WaitForChild("CookieCount")
local cookiecount = game.Workspace:WaitForChild("CookieStorage")

local children = cookiecount:GetChildren()
local count = #children
cookiecounter.Value = count

Thank you to anyone who can give me some advice/guidance

1 answer

✓ Accepted answer
local cookiecounter = game.ReplicatedStorage:WaitForChild("CookieCount")
local cookiecount = game.Workspace:WaitForChild("CookieStorage")

cookiecount.ChildAdded:Connect(function()
	cookiecounter.Value = #cookiecount:GetChildren()
end)
cookiecount.ChildRemoved:Connect(function()
	cookiecounter.Value = #cookiecount:GetChildren()
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.