How do i am can make a live count in my roblox game?

hello, i am trying to make a Tree live count like inside this game: MrBeast's #TeamTrees Live Tree Count - Roblox
i try make a script for that but that didnt work. here is the script that i try:

local hs = game:GetService(“HttpService”)

while true do wait(.2)

local response = hs:GetAsync(“https://teamtrees.org/”)

local data = hs:JSONDecode(response)

script.Parent.SurfaceGui.Amount.Text = tostring(data.items[1].statistics)

end

Well, there’s a few issues, one being that doing a get request returns HTML rather than JSON, so you’ll have to use the string library to do that.

Also, you can’t change SurfaceGuis (or any guis for that matter) from the Server, so you have to make a IntValue in the ReplicatedStorage and monitor changes from the client.

-- Server
local replicatedStorage = game:GetService("ReplicatedStorage")
local httpService = game:GetService("HttpService")
local count = replicatedStorage:WaitForChild("Count")

while true do 
    wait(1) -- do it in low intervals to prevent http throttling.
    local response = httpService:GetAsync("https://teamtrees.org/")
    local amount = string.match(response, "<div id=\"totalTrees\" class=\"counter\" data-count=\"(.-)\">") -- use the slashes to put strings inside a string
    count.Value = tonumber(amount) -- returns a number
end)
-- client
local replicatedStorage = game:GetService("ReplicatedStorage")
local count = replicatedStorage:WaitForChild("Count")
local surfaceGui = Path.To.SurfaceGui -- CHANGE THIS CHANGE THIS CHANGE THIS

count.Changed:Connect(function() -- changed for valueobjects only tracks when the value is changed
    surfaceGui.Amount.Text = tostring(count.Value)
end)
1 Like

hi. what you mean? “monitor changes” ?

I believe @mobyboyy means watching, or looking for changes in the count variable.

and is no works is just show in the text “Label” is how i put the scripts:

It seems like there might be something wrong with the changed event, it should be count.Value.Changed I believe, but if the script isn’t working I dont know why at the moment( I haven’t tested the script), but check out this thread:

2 Likes

Your issue is that the Client script isn't a LocalScript, @REALINONOOBYT.

It should be a LocalScript inside the StarterPack.

Also, you should do Play Solo when you test this.

2 Likes

You can actually change a SurfaceGui’s contents from a server script provided the server knows it exists. There isn’t any special treatment for replication of UI instances, only for the containers they exist in. In this case, I think I probably would just a server script to change the TextLabel in the SurfaceGui. I also wouldn’t recommend putting LocalScripts in StarterPack, but rather StarterPlayer.StarterPlayerScripts. This is more future-proof.

However, if you ever want to interact with said SurfaceGui, it does need to go under StarterGui and then can only be accessed by a client script.

Anyway, @REALINONOOBYT, if you want to go back to having a normal Script under the same Part that your SurfaceGui is in, use this code:

local HttpService = game:GetService("HttpService")

local URL = "https://teamtrees.org"
while true do
	local success, pageContents = pcall(function() return HttpService:GetAsync(URL) end)

	local treeCount = "Unable to Fetch Tree Count"
	if success then
		local wildcard, count = string.match(pageContents, "id%=\"totalTrees\"(.+)data%-count%=\"(%d+)\"%>")
		--The wildcard variable is there to help your code handle changes to their website's HTML later. It's unlikely that the data-count thing will ever
		--come before the id, but the other stuff in between the two attributes could change. Since we match it with (.+), we're actually getting two results
		--out of our string.match call. The first one we don't care about, but the second one contains the number we're after.
		if count ~= nil then
			treeCount = string.format("%d Trees Planted", tonumber(count)) --Ensure that the number is printed like a number.
		end
	end
	
	script.Parent.SurfaceGui.Amount.Text = treeCount
	
	wait(30) --Let's not update faster than this, I doubt whoever is running teamtrees.org wants you to spam them with requests that fast,
			 --and they might even be caching this result so it might not even update when you request it too fast.
end

Hopefully there’s enough explanation there that you can tell why I made the changes that I did.

3 Likes

thanks @mr_smellyman :slight_smile: (is works).

2 Likes