Where Would I Put This Code?

Hi there, I’m not sure where to put this code.

local function savePlayerData(playerUserId)
if sessionData[playerUserId] then
playerData:SetAsync(playerUserId, sessionData[playerUserId])
end
end

local function saveOnExit (player)
local playerUserId = “Player_” … player.UserId
savePlayerData (playerUserId)
end

game.Players.PlayerRemoving:Connect (saveOnExit)

Thanks for your time.

Please format your code correctly to make it easier to read

local function savePlayerData(playerUserId)
   if sessionData[playerUserId] then
      playerData:SetAsync(playerUserId, sessionData[playerUserId])
   end
end

local function saveOnExit (player)
   local playerUserId = “Player_” … player.UserId
   savePlayerData (playerUserId)
end

game.Players.PlayerRemoving:Connect (saveOnExit)

Now, to answer your question. It’s recommended to place scripts that run on the server to be placed in game.ServerScriptService.

2 Likes