I was playing around with Roblox's HTTP request system and YouTube's API, and I made a working system that displays your current subscriber count. But I've been trying for a long time and failing to make a system that displays the difference in subscribers in the console.
Is there any way I can implement this in my code?
local HttpService = game:GetService("HttpService")
local API_KEY = "SECRET" -- Replace with your API key
local CHANNEL_ID = "UCA4yZCVHlHZVknfesTU3XOg" -- Replace with your channel ID
local fonts = {
Enum.Font.Arcade,
Enum.Font.Arial,
Enum.Font.ArialBold,
Enum.Font.Bangers,
Enum.Font.Bodoni,
Enum.Font.Cartoon,
Enum.Font.Code,
Enum.Font.Creepster,
Enum.Font.DenkOne,
Enum.Font.Fantasy,
Enum.Font.Fondamento,
Enum.Font.FredokaOne,
Enum.Font.Garamond,
Enum.Font.Gotham,
Enum.Font.GothamBlack,
}
local function getChannelStats()
print("SENDING REQUEST")
local success, result = pcall(function()
local url = string.format("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=%s&key=%s", CHANNEL_ID, API_KEY)
local response = HttpService:GetAsync(url)
local data = HttpService:JSONDecode(response)
local subscriberCount = data.items[1].statistics.subscriberCount
--script.Parent.Text = "Inscritos: " .. subscriberCount
script.Parent.Text = subscriberCount
script.Parent.Font = fonts[math.random(1, #fonts)]
end)
if not success then
warn("Error fetching channel data:", result)
--script.Parent.Text = "Erro ao carregar dados."
end
end
-- Call the function initially and every 10 seconds
getChannelStats()
while true do
wait(2)
getChannelStats()
end
I tried doing it myself, using Roblox's AI, testing some things/code snippets, but nothing worked. The expectation was to display the difference in subscribers in the Roblox console using print()
You can store the previous YouTube subscriber count before it is updated, and write the difference in a TextLabel.
Changes:
... 22 lines skipped ...
+ local oldSubscriberCount = 0
... 9 lines skipped ...
+ local subscriberCountDifference = subscriberCount - oldSubscriberCount
+ local message = (subscriberCountDifference < 0 and "-" or "+") .. " subscribers"
+ -- Use the message variable to set the TextLabel of your choice
+ oldSubscriberCount = subscriberCount
... 19 lines skipped ...
Modified code:
local HttpService = game:GetService("HttpService")
local API_KEY = "SECRET" -- Replace with your API key
local CHANNEL_ID = "UCA4yZCVHlHZVknfesTU3XOg" -- Replace with your channel ID
local fonts = {
Enum.Font.Arcade,
Enum.Font.Arial,
Enum.Font.ArialBold,
Enum.Font.Bangers,
Enum.Font.Bodoni,
Enum.Font.Cartoon,
Enum.Font.Code,
Enum.Font.Creepster,
Enum.Font.DenkOne,
Enum.Font.Fantasy,
Enum.Font.Fondamento,
Enum.Font.FredokaOne,
Enum.Font.Garamond,
Enum.Font.Gotham,
Enum.Font.GothamBlack,
}
local oldSubscriberCount = 0
local function getChannelStats()
print("SENDING REQUEST")
local success, result = pcall(function()
local url = string.format("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=%s&key=%s", CHANNEL_ID, API_KEY)
local response = HttpService:GetAsync(url)
local data = HttpService:JSONDecode(response)
local subscriberCount = data.items[1].statistics.subscriberCount
local subscriberCountDifference = subscriberCount - oldSubscriberCount
local message = (subscriberCountDifference < 0 and "-" or "+") .. " subscribers"
-- Use the message variable to set the TextLabel of your choice
oldSubscriberCount = subscriberCount
--script.Parent.Text = "Inscritos: " .. subscriberCount
script.Parent.Text = subscriberCount
script.Parent.Font = fonts[math.random(1, #fonts)]
end)
if not success then
warn("Error fetching channel data:", result)
--script.Parent.Text = "Erro ao carregar dados."
end
end
-- Call the function initially and every 10 seconds
getChannelStats()
while true do
wait(2)
getChannelStats()
end