How to add player name to Datastore?

Hello! Now I made a script that teleports you to a game. (Easter egg) But people can join over and over again. And the charm of this is running away. (Info: If player tries to join again game not allows)

-- example code (server script with API Services enabled)
-- you would have to make modifcations to this, as this is only to show you what it should look like

local dss = game:GetService("DataStoreService") -- gets the data store service
local players = game:GetService("Players") -- players service

local dataStore = dss:GetDataStore("MyDataStore") -- gets your data store (can be whatever)

local function onPlayerJoined(player) -- on player joined function
   local store = dataStore:GetAsync("userids") -- get the contents of the data store with the requested key

   if table.find(store, player.UserId) then -- the data store is an array then search the array for the player's userid
      player:Kick() -- if they are in the array then kick then
  end

   dataStore:UpdateAsync("userids", function(old) -- update the data store if they weren't in it
      table.insert(old, player.UserId) -- add then to the data store
      return old -- return the data so it can update
   end)
end

player.PlayerAdded:Connect(onPlayerJoined) -- connect the function

https://developer.roblox.com/en-us/articles/Saving-Player-Data

1 Like

Rather than using player’s name, you should use their ID instead, since players can change their username. Their ID can easily be accessed with player.UserId

1 Like

Im having this error. Idk why its happening