Table seems to be wiping itself or failing to log entries

I have a table in a module script set to store Player names, and a bindable event. The problem is whenever I add an entry, and previous entries seem too not exist, and I have no clue why. Here is the code in question:

function States.AddPlayerToStates(Player,Event)
	if isInTable(Player,PlayerStates) then
		warn("Attempt to add already existing player to states table")
	else
		print(PlayerStates)
		PlayerStates[Player] = {}
		PlayerStates[Player]["Event"]=Event
		print(PlayerStates)
		print("Player added to states ".. Player)
		return Event.Event
	end
end

I have tried changing the variable from a module variable to a local one (Which it is now), and disabling the rest of the code in the script. Nothing seems to be working.

You reset the table back to an empty table:

Check if it exists first, then add the table if it doesn’t exist.

1 Like

Can we see the “isInTable” function? I believe that is the source of the problem.

1 Like

I wouldn’t think that would be the issue, because PlayerStates itself is resetting, unless that really is whats doing it.

local function isInTable(Needle, haystack)
	for _, element in pairs(haystack) do
		if element == Needle then
			return true
		end
	end
	return false
end

That’s the problem. In this case, the index would be what you are looking for. Unless you are using this function somewhere else, change it to this:

local function isInTable(Needle, haystack)
	for _, element in pairs(haystack) do
		if _ == Needle then
			return true
		end
	end
	return false
end

And if it really doesn’t matter whether it is the index or value, you could change it to this:

local function isInTable(Needle, haystack)
	for _, element in pairs(haystack) do
		if element == Needle or _ == Needle then
			return true
		end
	end
	return false
end
1 Like

It still wipes the base table.

local replicatedStorage = game:GetService('ReplicatedStorage')
local ServerScriptService = game:GetService("ServerScriptService")

local bindableEvents = ServerScriptService.ServerScriptService.BindableEvents

local function isInTable(Needle, haystack)
	for _, element in pairs(haystack) do
		if _ == Needle then
			return true
		end
	end
	return false
end


local States = {}


local PlayerStates = {}
States.PlayerCooldowns = {}



States["AddPlayerToStates"] = function(Player,Event)
	if isInTable(Player.Name,PlayerStates) then
		warn("Attempt to add already existing player to states table")
	else
		print(PlayerStates)
		PlayerStates[Player.Name] = {}
		PlayerStates[Player.Name]["Event"]=Event
		print(PlayerStates)
		print("Player added to states ".. Player.Name)
		return Event.Event
	end
end

return States

that should be the entire module script. What’s getting wiped is the PlayerStates table in it’s entirety. (Changed the function format to see if it would help solve the issue, it did not).

That’s quite weird. I’ll try this myself and see if I can identify the problem.

1 Like

Thank you. btw, the event variable is a bindable event created when the function is called.
I’m calling it like
States is the module script

local StateAdded = States.AddPlayerToStates(Player,Instance.new("BindableEvent"))

I did some tweaking, but there appears to be no problem for me?

1 Like

What happens if you try to add something else to the table? I’m using it to hold every players status effects, and I seem to get the most issues when I attempt to add a second player.

Adding another play does exactly what it’s supposed to for me.
image

1 Like

Devforum won’t allow me to edit my reply for some reason, but here’s the script if needed:

local replicatedStorage = game:GetService('ReplicatedStorage')
local ServerScriptService = game:GetService("ServerScriptService")



local function isInTable(Needle, haystack)
	for i, element in pairs(haystack) do
		if i == Needle then
			return true
		end
	end
	return false
end


local States = {}


local PlayerStates = {}
States.PlayerCooldowns = {}



States["AddPlayerToStates"] = function(Player,Event)
	if isInTable(Player.Name,PlayerStates) then
		warn("Attempt to add already existing player to states table")
	else
		print(PlayerStates)
		PlayerStates[Player.Name] = {}
		PlayerStates[Player.Name]["Event"]=Event
		print(PlayerStates)
		print("Player added to states ".. Player.Name)
		return Event
	end
end

return States
1 Like

I think it’s because I’m calling it from two seperate scripts.

That would be the reason. Each script would have separate PlayerStates.

1 Like

How would I make it so they shared a PlayerStates? I tried setting it to _G.PlayerStates but each script still gets its own copy.

Here:

local replicatedStorage = game:GetService('ReplicatedStorage')
local ServerScriptService = game:GetService("ServerScriptService")



local function isInTable(Needle, haystack)
	for i, element in pairs(haystack) do
		if i == Needle then
			return true
		end
	end
	return false
end


local States = {}


_G.PlayerStates = {}
States.PlayerCooldowns = {}



States["AddPlayerToStates"] = function(Player,Event)
	if isInTable(Player.Name,PlayerStates) then
		warn("Attempt to add already existing player to states table")
	else
		print(_G.PlayerStates)
		_G.PlayerStates[Player.Name] = {}
		_G.PlayerStates[Player.Name]["Event"]=Event
		print(_G.PlayerStates)
		print("Player added to states ".. Player.Name)
		return Event
	end
end

return States
1 Like

That still gives each of them a separate table.

It shouldn’t give them a separate table though?? _G is a global and would share across all scripts.

1 Like