How would I do this with table.insert?

I’m working on something new, and I’ve run into a problem. I’m working on exploit protection, and I don’t want to make remotes for every single person, so I want to make a table which will hold a username and the tick().

How would I do this?

I was going to use [Player.Name] = tick() but I don’t know how to add that with the script, without having it in the table beforehand.
Any alternatives I could use?

Thanks.

1 Like
Local array = {}
Game.Players.PlayerAdded:Connect(function(Player)
Table.insert(array,1,Player)
end

Currently it should save the player as an object, instead of it’s name. You might not need that.

Where do I put tick(), and how would I grab the value?

If you are planning on having this happen when the player joins, I would do this:

local playerTicks = {}

game.Players.PlayerAdded:Connect(function(plr)
      playerTicks[plr.UserId] = tick()
end)

game.Players.PlayerRemoving:Connect(function(plr)
      playerTicks[plr.UserId] = nil
end)

This will add the player to the dictionary. If you want to simulate a table that can be looped through in order, I recommend this module which would allow you to get the length of a dictionary so it can be looped through numerically.

To reference it, you would simply do lua playerTicks[plr.UserId]

Note - player.Name would work as well, I just prefer ID’s because their unique and have less potential of breaking in some way.

2 Likes

Instead, you can use

local Players = game:GetService(¨Players¨)

Players.PlayerAdded:Connect(function(Player)
--Code here.
end)

This lets you skip the process of turning the value of the player in the table to nil.

2 Likes

Thank you, this worked!

I made it so exploiters can’t spam a remote to get infinite money in a game, but your answer helped.

Thanks alot, I feel a bit dumb for forgetting about this.

Don’t worry, we all forget stuff forgot some random stuff from time to time!

Make sure to mark it as the answer so readers know it’s been answered!

1 Like