Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I keep someone's team when they leave?

I want to be able to set someone's team (i can do that), but then after they leave, I would like to be able to have it saved so when they join the next time, they are on that team. If you could help, that would be great!

1 comment

0

Use a DataStore, check the wiki for documentation and tutorials.

1 answer

✓ Accepted answer

You would use data-stores to save which team they were on. You can find more information on data-stores here.

For your particular case, it would be relatively simple. Just connect to the PlayerRemoving event and save to their data-store, and have it load their team on PlayerAdded.

Example Code:

local playerService = game:GetService("Players") -- game.Players so that we can connect to playeradded and playerremoving
local datastoreService = game:GetService("DataStoreService") -- datastores, roblox's way of saving data
local teams = game:GetService("Teams") -- teams are in here

local playersTeamDatastore = datastoreService:GetDatastore("Teams") -- teams can be anything, you can get any datastore

-- handle loading data when a player joins
playerService.PlayerAdded:Connect(function(player)
	local playerTeam = playersTeamDatastore:GetAsync(player.UserId) -- get their datastore value associated with their userid
	if playerTeam and teams:FindFirstChild(tostring(playerTeam))  then -- if they have a team saved
		player.Team = teams:FindFirstChild(tostring(playerTeam)) -- set their team
	end
end)

-- handle saving data when a player leaves
playerService.PlayerRemoving:Connect(function(player)
	local playerTeam = player.Team -- check what team they were on when they left
	playersTeamDatastore:SetAsync(player.UserId, playerTeam) -- save their team
end)

edit: i wrote "and if" for some reason, fixed

This discussion is preserved as it was. New answers, comments, and votes are closed.