I have a Player class, and I am wondering how I would use the player class, however I remembered that you can index a table using tables, so I was wondering if you could do the same with an instance, and then also use that index to call some of my methods, like :Delete() shown in the code?
You’re absolutely on the right track! Using instances as keys in a table is a common and powerful way to associate data with specific instances. In your case, you’ve set up a table called players to store instances of your Player class, associated with Roblox players.
The code you’ve provided for handling player joining and leaving events is a great start. When a player joins, you create a new instance of your Player class and store it in the players table using the player as the key. And when a player leaves, you can call methods like :Delete() using the stored instance.
Here’s an example of how you could use it:
local players = {}
game.Players.PlayerAdded:Connect(function(player)
-- Create a new Player instance and associate it with the player
players[player] = Player.new()
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerInstance = players[player]
if playerInstance then
-- Call the :Delete() method on the associated instance
playerInstance:Delete()
end
end)
In this example, when a player leaves, you check if the associated instance exists and then call :Delete() on it.
This approach allows you to use instances as keys in a table and leverage their methods. It’s a powerful way to manage data for different in-game entities associated with players. If you have any more questions or need further assistance, feel free to ask. We’re here to help!