Is there a way to check if a player joined the group?

I’m trying to make a leaderboard show players that joined a group by using ID.

This is the server script in ServerScriptService:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local GroupsService = game:GetService("GroupService")

local groupId = 16922207

local GroupMembersDataStore = DataStoreService:GetDataStore("GroupMembers")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local groupMembers = Instance.new("IntValue")
	groupMembers.Name = "GroupMembers"
	groupMembers.Parent = leaderstats

	leaderstats.Parent = player

	local success, data = pcall(function()
		return GroupMembersDataStore:GetAsync(tostring(player.UserId))
	end)

	if success then
		if data then
			groupMembers.Value = data.members or 0
		end
	else
		warn("Error loading data for player", player.Name)
	end

	local groupMembership = GroupsService:IsUserInGroup(groupId, player.UserId) -- there is an error in IsUserInGroup
	if groupMembership then
		groupMembers.Value += 1

		local success, error = pcall(function()
			GroupMembersDataStore:SetAsync(tostring(player.UserId), {members = groupMembers.Value})
		end)

		if not success then
			warn("Error saving data for player", player.Name, "Error:", error)
		end
	end
end)

the debugger tells me that IsUserInGroup is not valid, that’s why i need an alternative to this.

Thanks for your time.

3 Likes
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local groupId = 16922207

local GroupMembersDataStore = DataStoreService:GetDataStore("GroupMembers")

Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local groupMembers = Instance.new("IntValue")
    groupMembers.Name = "GroupMembers"
    groupMembers.Parent = leaderstats

    leaderstats.Parent = player

    local success, data = pcall(function()
        return GroupMembersDataStore:GetAsync(tostring(player.UserId))
    end)

    if success then
        if data then
            groupMembers.Value = data.members or 0
        end
    else
        warn("Error loading data for player", player.Name)
    end

    if player:IsInGroup(groupId) then
        groupMembers.Value += 1

        local success, error = pcall(function()
            GroupMembersDataStore:SetAsync(tostring(player.UserId), {members = groupMembers.Value})
        end)

        if not success then
            warn("Error saving data for player", player.Name, "Error:", error)
        end
    end
end)

Players.PlayerRemoving:Connect(function(player)
    -- Optionally save data on player removing
    local groupMembers = player:FindFirstChild("leaderstats"):FindFirstChild("GroupMembers")
    if groupMembers then
        local success, error = pcall(function()
            GroupMembersDataStore:SetAsync(tostring(player.UserId), {members = groupMembers.Value})
        end)

        if not success then
            warn("Error saving data for player", player.Name, "Error:", error)
        end
    end
end)
4 Likes

I think you meant to use Player:IsInGroup().

2 Likes

it worked, wow thanks, It shows the leaderboard in the leaderboard player side.

2 Likes

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