"table index is nil"

never ran into this issue before…

local Manager = {}

data = {};
client = game.ReplicatedStorage.Remotes.ClientServer
ps = game:GetService("Players")
local framework = require(script.Parent)

function Manager:Add(char)
	local plr = ps:GetPlayerFromCharacter(char)
	if plr then
	data[plr.UserId] = framework.new()
	else
	data[char] = framework.new()
	end
end

function Manager:Remove(char)
	local plr = ps:GetPlayerFromCharacter(char)
	if plr then
	data[plr.UserId] = nil
	else
	data[char] = nil
	end
end

function Manager:Retrieve(char)
	local plr = ps:GetPlayerFromCharacter(char)
	if plr then
	return data[plr.UserId]
	else
	return data[char]
	end
end

function Manager:Init()
	ps.PlayerAdded:Connect(function(plr)
		Manager:Add(plr.Character)
	end)

	ps.PlayerRemoving:Connect(function(plr)
		Manager:Remove(plr.Character)
	end)
end

client.OnServerInvoke = function(plr)
return Manager:Retrieve(plr.Character)
end

return Manager

Could you like show us where the error is occurring? Because they’re a lot of table modifications in your script

In the Add Function. abcdefghijk

char is nil, whatever called Add passed nil to it

2 Likes

Oh my god I’m so dumb, how did I not realise that? Anyway @hamncheese call the add function only after the character is added, like this

game.Players.PlayerAdded:Connect(function(plr)
   local char = plr.Character or plr.CharacterAdded:Wait()
   Manager:Add(char)
end)
1 Like

right as the other dude said char was nil I immediately thought of doing this but you said it before I did it so just take the W lol.

Man mark @1waffle1 as the solution, he figured it out :+1:

:100: I respect it bro. charrrrrrrrr

well i take it back that wasn’t the issue but you got my brain working. It was because I’m using oop to do it, I forgot to put return self in the actual module.

  1. Also there was an error that popped up because the char was nil so you did basically help.

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