Get the number of trees planted from #teamtrees using HttpService

Hi, I’m trying to add a simple counter in my game that shows the current number of trees planted by teamtrees.org! I really support their cause, so I wanted to add a large tree in my game with a sign next to it showing the current amount of trees planted (updated every 60 seconds)

I’ve already got the site’s data using HttpService:GetAsync() and was able to put all that data on my sign every 60 seconds, the issue is I have no idea how to make any sense of the data and only get the number of trees planted.

My Script:

local HttpService = game:GetService(‘HttpService’)
local URL = ‘https://teamtrees.org/

function GetData()
local Data
pcall(function()
Data = HttpService:GetAsync(URL)
end)
if Data then
return Data
end
end

while true do
wait(60)
local Data = GetData()
if Data then
script.Parent.TextLabel.Text = 'trees planted: '…Data… ‘\ngo #teamtrees!’
end
end

9 Likes

I actually just made this exact thing! It’s by no means perfect, but works for the time being. You can check it out here:

https://gyazo.com/7cada5b4648602e1fa39ec0ae4e3b4da

6 Likes

That’s awesome! I’m unable to take a copy though?

Oops, sorry. Forget to put it on sale. Check now!

1 Like

This is just what I needed, thank you!

It seems this is no longer working! How can I fix this?

1 Like

Sorry for the late response, I didn’t see your message.
It should be fixed now. It’s published to the same model as before. You can also change line line that contains

local Trees1 = string.sub(Trees,2824+99,2824+99+10)

to

local Trees1 = string.sub(Trees,6436,6436+10)

and it should start working again.

2 Likes

Thank you! Could you by any chance explain how this works and how I could do it myself?

Thanks, it looks amazing. How did you get the numbers in a custom font though?

He gets the characters between 6436 and 6436+10, which is the text showing the trees planted. I don’t personally recommend this because any small html change, whitespace change, etc will cause it to break and need updating.

Yeah, it’s not a good solution. I don’t recommend this for anything long-term as there’s almost a 100% chance it will break. I just hacked this whole thing together due it’s short-term nature.

So how would I get around this?

You could possibly use string.find() to find the text around the number you’re looking for, rather than going strictly based off of character count.

1 Like

in js, something like FindElementById would be useful. I’m sure you can string something together with string.find like CoderCrazy suggested.

In lua, your best bet is of course string.find() as @CoderCrazy says above.
However, if you’re willing to get the web alternative, there are many web parsers/scrapers that would allow you to parse the HTML and directly retrieve the value.

Adding on to string.find(), here is some JavaScript code that uses regex to find the tree count. You could fairly easily convert this to lua.

2 Likes