Playercount Datastore acting weird

Hey Developers!

I’ve been recently trying to develop a hub to get to all my football team fields, however, I want to make it that you can see the player count on each billboard UI.

. No, I’m not asking to be spoon-fed. I already have something that I think should work.

function playerCount()
    local success, servers = pcall(function()
        return playerCountStore:GetSortedAsync(true,100)
    end)
    local places = servers:GetCurrentPage()
    for i, place in pairs(places) do
        local id = place.key
        local count = place.value
        if game.ReplicatedStorage.PlayerCount:FindFirstChild(id) ~= nil then
            game.ReplicatedStorage.PlayerCount:FindFirstChild(id).Value = count
        else
            local val = Instance.new("IntValue",game.ReplicatedStorage.PlayerCount)
            val.Name = id
            val.Value = math.max(0,count)
        end
    end
    return success
end

game.ReplicatedStorage.Events.PlayerCount.OnServerInvoke = playerCount

And this in another script:

while true do
    wait(1)
    for _,v in pairs(Pages.Stadiums.List:GetChildren()) do
        if v:IsA("Frame") then
            game.ReplicatedStorage.Events.PlayerCount:InvokeServer()
            if game.ReplicatedStorage.PlayerCount:FindFirstChild(v.PlaceID.Value) ~= nil then
                v.Online.Text = game.ReplicatedStorage.PlayerCount:FindFirstChild(v.PlaceID.Value).Value.." Players Online"
            end
        end
    end
    
end

However, when the script runs in the game, it keeps saying, “two many request, try running fewer requests”

Now, I don’t know how to fix this, or ‘send fewer requests’.

I started to research and I saw this post talking about it, but nobody solved anything.

Please help.

You are calling the datastore thing once every second, per player. (60 per minute for every player)
From here, the limit for calls to GetSortedAsync is 5 per minute, + 2 for every player.

Instead of updating it whenever the OnServerInvoke is called, instead have it in a while loop that cycles every 15 seconds or so, then just return the stored value whenever the player requests it.

(Also, you should be careful about servers crashing. If they crash without the BindToClose thing firing, you may have a phantom player count.)

1 Like

Thanks for responding!

Question, i’m quite new to scripting, so how would I loop cycle? Would I do a wait somewhere around “local success”?

I found the solution, instead of

while true do

I did

while wait(200) do

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.