Issues with table.find

Hey

I’ve been researching this topic as much as I could, however I can’t find any topic relating to mine, so I’ll make my own

Code:

local playing = {}

game.Players.PlayerAdded:Connect(function(player)
    if not table.find(playing, player.Name) then
        table.insert(playing, player.Name)
        warn('Inserted player directly!')
    else
        warn('Player is already in the table!')
    end
end)


game.Players.PlayerRemoving:Connect(function(plr)
    if table.find(playing, plr.Name) then
        table.remove(playing, plr.Name)
        warn('Player has been removed from Playing Table!')
    else
        warn('Player is leaving successfully! No table issues')
    end 
end)

This code is rather simple; It’s supposed to check for any new players, and add them to a table if they aren’t already inside of that table. Then, once they leave it should remove them.

However, I can’t seem to add them to the table in the first place. Any help would be really appreciated :blush:

Is this in a local script or a normal one?

It’s in a normal script.

For now I’ve resided to delete it all and do

local players = game.Players:GetPlayers()) 

instead of all I had before, but I’m still curious as of to why it didn’t work

Weird, I tried it out myself but for some reason, it worked fine.

Hmm, thats super weird in fact. Well, I got it sorted using a different method atleast. Thanks for your help though! :blush:

Are you sure this is the full script? Make sure that there are not other factors that could break the code, or maybe the script being on the wrong place

It’s just a code segment for my full script. I’m sure there is something preventing it from working as you are saying. However, I’ll test it out in an empty script sometime soon😊

The full script is around 1.5k~ lines, so I’m not gonna post it here :slight_smile:

Tell me if that fixed the problem.

table.remove expects an index, not a value to remove.

The correct code would’ve been:

game.Players.PlayerRemoving:Connect(function(plr)
    if table.find(playing, plr.Name) then
        table.remove(playing, table.find(playing, plr.Name))
        warn('Player has been removed from Playing Table!')
    else
        warn('Player is leaving successfully! No table issues')
    end 
end)

Since table.find returns an index.

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