Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
H34E7R 19
6 years ago

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!

0
Use a DataStore, check the wiki for documentation and tutorials. gullet 471 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

Ad

Answer this question