Created
February 3, 2022 13:22
-
-
Save eldonwilliams/5c48904cfc114a864711d645aa1cfd2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- CONFIG | |
local RequestWaitTime: number = 5 | |
local RequestURL: string = "https://eldon.zone/api/roblox/games/2316994223" | |
local MemoryStoreService = game:GetService("MemoryStoreService") | |
local Messaging = game:GetService("MessagingService") | |
local RunService = game:GetService("RunService") | |
local HttpService = game:GetService("HttpService") | |
local FavoritesProvider = MemoryStoreService:GetSortedMap("FAVORITES") | |
local Knit = require(game:GetService("ReplicatedStorage"):WaitForChild("Packages"):WaitForChild("Knit")) | |
local ReplicaService = require(game:GetService("ReplicatedStorage"):WaitForChild("Packages"):WaitForChild("ReplicaService"))["ReplicaService"] | |
local Favorites = Knit.CreateService({ | |
Name = "Favorites", | |
}) | |
Favorites.IsProvider = false | |
Favorites.Count = ReplicaService.NewReplica({ | |
ClassToken = ReplicaService.NewClassToken("FavoritesCount"), | |
Data = { Count = nil, }, | |
Replication = "All", | |
}) | |
function Favorites:KnitInit() end | |
function Favorites:KnitStart() | |
if (FavoritesProvider:GetAsync("Provider") == nil) then | |
Favorites:SetupAsProvider() | |
else | |
Favorites:SetupAsListener() | |
end | |
end | |
function Favorites:SetupAsListener() | |
Favorites.IsProvider = false | |
local connections: Array<RBXScriptConnection> | |
table.insert(connections, Messaging:SubscribeAsync("FavoritesNewProvider", function() | |
for _, connection in pairs(connections) do | |
connection:Disconnect() | |
end | |
Favorites:SetupAsProvider() | |
end)) | |
table.insert(connections, Messaging:SubscribeAsync("FavoritesUpdate", function(newFavorites) | |
Favorites.Count:Set("Favorites", newFavorites) | |
end)) | |
end | |
function Favorites:SetupAsProvider() | |
Favorites.IsProvider = true | |
print("provider") | |
local providerFound = false | |
local success: boolean, result: string = pcall(function() | |
FavoritesProvider:UpdateAsync("Provider", function(old) | |
if (old) then | |
providerFound = true | |
return old | |
end | |
return game.JobId | |
end, 2592000) | |
end) | |
if (providerFound) then | |
Favorites:SetupAsListener() | |
return | |
end | |
if (not success) then | |
print("Error setting Provider for Favorites") | |
print(result) | |
return | |
end | |
local function loopStepped() | |
local response = HttpService:GetAsync(RequestURL, true) | |
response = HttpService:JSONDecode(response) | |
Favorites.Count:SetValue({"Count"}, response.visits) | |
Messaging:PublishAsync("FavoritesUpdate", response.visits) | |
end | |
local loopTime = 0 | |
local stepCount = 0 | |
RunService.Heartbeat:Connect(function(delta) | |
loopTime += delta | |
if (loopTime / RequestWaitTime > stepCount) then | |
stepCount += 1 | |
loopStepped() | |
end | |
end) | |
game:BindToClose(function() | |
local closed: boolean, closedResult: string = pcall(function() | |
Messaging:PublishAsync("FavoritesNewProvider", true) | |
FavoritesProvider:RemoveAsync("Provider") | |
end) | |
if (not closed) then | |
print("Oh no") | |
print(closedResult) | |
end | |
end) | |
end | |
return Favorites |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment