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.
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)