Script that figures out how many people are at a different place

Im making a Nascar game and all the tracks are in different places under the same game.
Im trying to script something that sees how many players are playing that track.

I want to do this so players will now where most people are so they will have better fun

How would I do this?

1 Like

Can you post your existing code? I’m not going to make it for you.

2 Likes

I dont have existing code but I found this, https://games.roblox.com/docs#!/Games/get_v1_games how can I use an API in my game?

Well, you could add a brick in the cars and you can refer to the parts and find their positions.

1 Like

? Not what im trying to figure out, I want to know how many people are in a diffrent game

1 Like

Ohh, sorry. I didn’t really read what you said. Also look here. There may be some helpful tips on how to do this.

https://devforum.roblox.com/search?context=category&context_id=55&q=Server%20list&skip_context=false

1 Like

Sadly that didnt help… do you know how to use the messaging service to 1. read how many players are in the current game 2. Transfer it to the main game/ starting place?

It sounds like you just need the messaging service. Could use some web-programming as well but that doesn’t sound like something you’d want. However, I need to ask why you would want this though?

1 Like

My game is small and needs its annoying to go to every race track just to see which one people are playing at.

You can use this API endpoint, which returns all of the servers and information of a place, and iterate through the JSON-decoded code and add all of the players playing specified playing element / entry of the table.

Note: You can’t send requests to roblox domain from studio or the roblox client, so in the request URL, change roblox.com to rprxy.xyz, which is a proxy server used to send requests from roblox territory

Request url:

https://games.roblox.com/PLACEID/Public?sortOrder=Asc&limit=100

This endpoint will return a maximum of 100 servers, but if you exceed this amount, the nextPageCursor element will not be an empty string, and you can proceed to the next page using the same request URL, but adding this at the end of the URL:

&cursor=NEXTPAGECURSOR

Basically, you continue to reiterate until the nextPageCursor element is an empty string, meaning there are no more pages of servers

DO you think messaging service could be just as good?
How does this look?

local ms = game:GetService("MessagingService")
ms:SubscribeAsync("PLRCOUNT",function(message)
   plrcount = message.data
end)

game.Players.PlayerAdded:Connect(function()
   plrcount = plrcount+1
   ms:PublishAsync("PLRCOUNT",plrcount)
end)
game.Players.PlayerRemoving:Connect(function()
   plrcount = plrcount-1
   ms:PublishAsync("PLRCOUNT",plrcount)
end) 

function callbackFunction(serviceData)
   local InGame= httpService:message(serviceData.data)

   script.Parent.Text = InGame
end)

There are limitations with both implementations, but I will talk about why using httpservice isn’t viable. One, it is quite slow because it goes through a serialisation algorithm by Roblox and quite honestly, it has a dependency on the site which is a factor that could ruin gameplay. If reliability is desired then messaging service is better in my opinion.

Since you only need to carry very little information, there’s no reason as to why you should overkill and go beyond.

1 Like

You will want to include the placeId in the request string:

-- Source place (start place / hub menu)
local PLACE_IDS = {
-- list of place ids
}

local PLACE_COUNT = {

}

for _, id in ipairs(PLACE_IDS) do
    PLACE_COUNT[id] = 0

    ms:SubscribeAsync("PLRCOUNT_" .. id, function(message)
        PLACE_COUNT[id] = tonumber(message)
    end)
end

-- Subordinate place (track places)

game.Players.PlayerAdded:Connect(function()
    plrcount += 1
    ms:PublishAsync("PLRCOUNT_" .. game.PlaceId, tostring(plrcount))
end)

game.Players.PlayerAdded:Connect(function()
    plrcount -= 1
    ms:PublishAsync("PLRCOUNT_" .. game.PlaceId, tostring(plrcount))
end)
1 Like

Didnt work… I used the same script, defined the things that werent defined and it didnt work…

There’s several ways you can do this.

  • Magnitude
  • Touched Events
  • GetTouchingParts
  • Region3

Magnitude

local Players = game:GetService("Players")

local startPosition = Vector3.new() --where the 3d position should originate at
local maxDistance = 10 --how close a player must be to a specific position to get counted
local function checkDistance(position0, position1)
    if ((position0 - position1).Magnitude < maxDistance) then
        return true
    end
    return false
end

local function getPlayersFromStartPosition()
    local playersInPlace = {}
    for _,v in next, Players:GetPlayers() do
        local character = v.Character
        if (character) then
            local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
            if (humanoidRootPart) then
                if (checkDistance(startPosition, humanoidRootPart.Position)) then
                    table.insert(playersInPlace, v)
                end
            end
        end
    end
    return playersInPlace
end

Touched Events

local part = workspace.Part --please point this to a specified part

local playersInPlace = {}; do --doing the magic
	part.Touched:Connect(function(child)
		for _,v in next, Players:GetPlayers() do
			local character = v.Character
			if (character) then
				if (child:IsDescendantOf(character)) then
					table.insert(playersInPlace, v)
				end
			end
		end
	end)
	part.TouchEnded:Connect(function(child)
		for _,v in next, Players:GetPlayers() do
			local character = v.Character
			if (character) then
				if (child:IsDescendantOf(character) and table.find(playersInPlace, v)) then
					table.remove(playersInPlace, table.find(playersInPlace, v))
				end
			end
		end
	end)
end

local function getPlayersInPlace()
	return playersInPlace
end

GetTouchingParts

local Players = game:GetService("Players")

local part = workspace.Part --please set this to your correct target part

local function buildPlayersInPlace(fromPart)
    local playersInPlace = {}
    for _,child in next, fromPart:GetTouchingParts() do
        for _,v in next, Players:GetPlayers() do
            local character = v.Character
            if (character) then
                if (child:IsDescendantOf(character) and table.find(playersInPlace, v)) then
                    table.remove(playersInPlace, table.find(playersInPlace, v))
                end
            end
        end
    end
    return playersInPlace
end
local function getPlayersInPlace()
    return buildPlayersInPlace()
end

Region3

local origPosition = Vector3.new(0, 0, 0) --please set this to the appropriate position
local origSize = Vector3.new(4, 4, 4) --please set this to the appropriate size

local function findPartsInRegion3(partPosition, partSize)
    local minPosition = partPosition - partSize/2
    local maxPosition = partPosition + partSize/2
    return workspace:FindPartsInRegion3(
        Region3.new(
            minPosition,
            maxPosition
        )
    )
end
local function getPlayersInPlace()
    local playersInPlace = {}
    for _,v in next, findPartsInRegion3(origPosition, origSize) do
        for _,x in next, Players:GetPlayers() do
            local character = x.Character
            if (character) then
                if (v:IsDescendantOf(character)) then
                    table.insert(playersInPlace, x)
                end
            end
        end
    end
end

Please consider incorporating your own system. This is just a few examples of how you can do this process.

OP is trying to find a way to check how many people are in a seperate game/place, not a physical location in a single game.

1 Like

Oh wait, I completely missed the main topic.
I apologize.

1 Like

Perhaps you could make a Script that just loops through all of the players in the server, counts them, and then uses MessagingService to send an intValue storing the number of players across to the other servers. It would significantly lower the use of MessagingService in comparison to updating the player count every time someone joins/leaves - the different approaches really depends on the use-case in this scenario. I’ve never used MessagingService myself, so I can mostly just help with gathering the number of players.

Perhaps something like this:

-- Run the below every time you need the player count
local plrcount = 0 -- Define the number at first.

for i,v in pairs(game.Players:GetChildren()) do
if v:IsA("Player") then -- Check if it's a player.
       plrcount = plrcount+1 -- Update the player count.
    end
end

-- Use MessagingService to send plrcount as an IntValue
2 Likes

Any errors? Did you use the appropriate IDs?

Used right IDS, No errors… Ill check again