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