Table data randomly getting destroyed?

Hello! Recently I’ve been having an issue with a group-getting script (to minimize Http requests). The script works perfectly in my small test game, but when I put it inside of the actual game which has much more going on, it totally breaks.

The error occurring is shown in the picture below:

3f70f773fedbebeefccc3c5d2402448d

It is also worth mentioning that in getTable(), the print does indeed print the table with the player data. However, later it prints “is nil”, and when getTable() is supposed to run again, it remains nil. I’m so confused honestly.

local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetRankInGroup = ReplicatedStorage.Bindables:WaitForChild("GetRankInGroup")
local GetRoleInGroup = ReplicatedStorage.Bindables:WaitForChild("GetRoleInGroup")
local IsInGroup = ReplicatedStorage.Bindables:WaitForChild("IsInGroup")

local plrGroupData = {}

local function getTable(plr)
	repeat wait() pcall(function() plrGroupData[plr.Name] = GroupService:GetGroupsAsync(plr.UserId) end) until plrGroupData[plr.Name] ~= nil
	print(plrGroupData[plr.Name])
end

local function plrAdded(plr)
	getTable(plr)
	local character1 = plr.Character or plr.CharacterAdded:Wait()
	plr.CharacterAdded:Connect(function()
		getTable(plr)
	end)
end

local function plrRemoving(plr)
	if plrGroupData[plr.Name] then
		plrGroupData[plr.Name] = nil
	end
end

Players.PlayerAdded:Connect(plrAdded)
Players.PlayerRemoving:Connect(plrRemoving)

GetRankInGroup.OnInvoke = function(groupId, plr)
	if plrGroupData[plr.Name] == nil then
		plrGroupData[plr.Name] = getTable(plr)
		print("is nil")
	end
	print(plrGroupData[plr.Name])
	for _,group in pairs(plrGroupData[plr.Name]) do
		if group.Id == groupId then
			return group.Rank
		end
	end
	return 0
end

IsInGroup.OnInvoke = function(groupId, plr)
	if plrGroupData[plr.Name] == nil then
		plrGroupData[plr.Name] = getTable(plr)
		print("is nil")
	end
	for _,group in pairs(plrGroupData[plr.Name]) do
		if group.Id == groupId then
			return true
		end
	end
	return false
end

GetRoleInGroup.OnInvoke = function(groupId, plr)
	if plrGroupData[plr.Name] == nil then
		plrGroupData[plr.Name] = getTable(plr)
		print("is nil")
	end
	for _,group in pairs(plrGroupData[plr.Name]) do
		if group.Id == groupId then
			return group.Rank
		end
	end
	return "Guest"
end

Is there any reason that you aren’t using this method?

Also, can you show the line where you call Invoke on the “IsInGroup” BindableFunction?

1 Like

Hey man. Thank you for your response. I did end up fixing this like a minute ago. The issue occurred where I defined:

if plrGroupData[plr.Name] == nil then
		plrGroupData[plr.Name] = getTable(plr)
		print("is nil")
	end

Since getTable() doesn’t actually return anything, it was setting the data to nil.

Thank you for your hasty response, though.

1 Like

Mark your post as the solution.

Awesome. Looks like we need some sleep after both of us overlooked that haha

For real man. Sleep well! Seems my logic has taken a turn for the worst lol.

1 Like