My OtherPlace Player Count code

Hello, I have a WORKING player count code with HTTP. This makes a sign and the sign’s text shows the amount of players in one my my minigames. (Part of my experience overall)
Right now, it only updates if you leave and rejoin. This is okay, but somewhat annoying.
I want this to update immediately on demand when ever the player count changes.
My code is here.
You may copy paste this code if you don’t know how to use http. :slight_smile:

local parent1 = script.Parent
local HttpService = game:GetService("HttpService")
local function GetUniverseId(GameId)
	local URL = "https://api.roproxy.com/universes/get-universe-containing-place?placeid=".. GameId
	local Response = HttpService:GetAsync(URL)
	Response = HttpService:JSONDecode(Response).UniverseId
	return Response	
end
local function GetServers(GameId)


	local response = HttpService:JSONDecode(HttpService:GetAsync("https://games.roproxy.com/v1/games/" .. GameId.."/servers/Public?sortOrder=Asc&limit=100" ))
	return response

end
local function GetPlaying(Servers)
	local Playing = 0
	for i,Server in pairs(Servers) do

		local ServerPlaying = Server[1]["playing"]
		if ServerPlaying then
			print('f')
			Playing += ServerPlaying
		end
	end
	return Playing
end
while true do


	local QUERY_URL = "https://games.roproxy.com/v1/games?universeIds="

	local GameId = 8104728938-- change to place id

	local Servers = GetServers(GameId)
	GameId = GetUniverseId(GameId)

	if Servers then
		local Playing = GetPlaying(Servers)
		-- "https://games.rprxy.xyz/v1/games?universeIds=000000"


		print(Playing)
		script.Parent.Text = "Players: " .. Playing
	end

	task.wait(5)
end

personally i think it looks good(considering it’s the same code my co-worker uses). But I do think you should use string.format instead. Like this

local parent1 = script.Parent
local HttpService = game:GetService("HttpService")
local function GetUniverseId(GameId)
	local URL = "https://api.roproxy.com/universes/get-universe-containing-place?placeid=".. GameId
	local Response = HttpService:GetAsync(URL)
	Response = HttpService:JSONDecode(Response).UniverseId
	return Response	
end
local function GetServers(GameId)


	local response = HttpService:JSONDecode(HttpService:GetAsync("https://games.roproxy.com/v1/games/" .. GameId.."/servers/Public?sortOrder=Asc&limit=100" ))
	return response

end
local function GetPlaying(Servers)
	local Playing = 0
	for i,Server in pairs(Servers) do

		local ServerPlaying = Server[1]["playing"]
		if ServerPlaying then
			print('f')
			Playing += ServerPlaying
		end
	end
	return Playing
end
while true do


	local QUERY_URL = "https://games.roproxy.com/v1/games?universeIds="

	local GameId = 8104728938-- change to place id

	local Servers = GetServers(GameId)
	GameId = GetUniverseId(GameId)

	if Servers then
		local Playing = GetPlaying(Servers)
		-- "https://games.rprxy.xyz/v1/games?universeIds=000000"


		print(Playing)
		script.Parent.Text = string.format("Players: %.f", Playing)
	end

	task.wait(5)
end

This should probably go to scripting support.

Isn’t that the purpose of the while true do loop? It should be updating every 5 seconds, you can’t test it if you’re the only player because the label will be stuck at 1.

1 Like

Well, I could leave and rejoin and it won’t update from 1-0 unless my leafve and rejoin from my test session in studio.
@Forummer