Making Teams Permanent

I’m currently making a game with 3 teams. When the player plays for the first time they will spawn on a team called No Team. How do I make it so when they touch a brick they will change to that team. However I want them to be permanently stuck on that team (When the leave and come back they will spawn on that team and there’s no way for them to join a different team).
Thanks for your time

3 Likes

For this you would need to store the players team in a DataStore when they leave, the value dependent on the team.

And when the player joins it will retrieve that value and move them to the corresponding team of their value.

4 Likes

When I get home, I will write a script, not for DataStore, but to trigger the team change. I will write this if someone doesn’t, because I will get home in a while.

2 Likes

Actually I just got home and realized, this does not need a script; for the team-change. Create a “SpawnLocation” and go to “TeamColor” and change it to your desired “Team”'s “TeamColor”. Then change the setting on the “SpawnLocation”, “AllowTeamChangeOnTouch” to ‘true’. I do not know the rest of the variables in your game, so I will not write a data-store script, but this is how I can help you. For an idea in the future, maybe you can make a function that triggers when a player steps on the “SpawnLocation” then saves their team, and makes true to a “BoolValue”'s value, that they have used up their team-change; you can save this value as-well. I am not experienced much in data-stores, though this is as much as I can help you, for now I guess; I hope this works as a solution for you! If you need to learn how to write data-stores, you can watch a quick video tutorial, or get one from the ToolBox; maybe even ask others on another thread! Thanks for being patient with me while I got home! :blush:

4 Likes

How do I get a tutorial from the toolbox. Thanks for the help btw.:grinning:

1 Like
local DataStore = game:GetService("DataStoreService"):GetDataStore("Teams")

local function OnJoin(Player)
  local Success, Team = pcall(function() return DataStore:GetAsync(Player.UserId) end)
  if Success and Team and game.Teams:FindFirstChild(Team) then
      Player.Team = game.Teams[Team]
  else
      warn("Player has no Team")
      spawn(function()
         repeat wait() until Player.Team ~= game.Teams["StarterTeam"]
         local Success, MaxTries, Tries = false, 5, 0
         repeat
             Success = pcall(function() DataStore:SetAsync(Player.UserId, Player.Team.Name) end)
             Tries = Tries + 1;
              wait(1.5)
          until Success or Tries == MaxTries
          if MaxTries == Tries then warn("Couldn't save data") end
       end)
    end
end

game.Players.PlayerAdded:Connect(OnJoin)

This was all written in the box, hasn’t been tested but I think an approach similar to this should work for the base purpose.

3 Likes

Where would I put this script?

1 Like

Set it in Workspace i guess, or in ServerScriptService.

2 Likes

No disrespect but your script seems to have some possible issues/bad scripting habits, and doesn’t really answer OP’s question.
So basically, when a player touches a brick, their team will change to the appropriate once and be locked to that team permanently even when they leave and rejoin.
The best way to do this is just set their team and save it as soon as they touch the part (and make sure they don’t have a team yet), then load their team back.
Here’s some untested code to help get you started:
(Sorry it’s indented weirdly I’m new to devforum)

local datastore = game:GetService("DataStoreService"):GetDataStore("Teams")

for _,v in ipairs(workspace.TeamChangers:GetChildren()) do
v.Touched:connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p then
		if p.Team == game:GetService("Teams"):FindFirstChild("No team") then
			p.Team = game:GetService("Teams"):FindFirstChild(v.Name)
			local s,e = pcall(function()
				datastore:SetAsync(p.UserId,v.Name)
			end)
			if e then warn(e) end
		end
	end
end)
end
game:GetService("Players").PlayerAdded:connect(function(plr)
local data = nil
local s,e = pcall(function()
	data = datastore:GetAsync(plr.UserId)
end)
if e then warn(e) end
if data then
	plr.Team = game:GetService("Teams"):FindFirstChild(data)
end
end)
5 Likes

Im kind of confused by your script is there any way you could break it down and explain it please? Thanks.

1 Like

I’m not good at explaining sorry. If you’re confused about a specific part I can try to explain it better.

1 Like

the “No teams” part, do I change it to my team name?

1 Like

For this, you are going to need to look into this page, specifically the SetAsync, and GetAsync functions of the DataStoreService.

  • You will need to get the name of the team that the player is on when they leave the game using the game.Players.PlayerRemoving function.

  • Then save it to a datastore with SetAsync, and then when the player joins get the data with GetAsync and team them to the proper team.

Setting the team when the block is touched is something to be careful about, this could lead to your DataStore requests being dropped because you’re throttling it.

The best way to come at this, is to save their team when they leave the game, so your DataStore saves the players userId as the key only once, to prevent data loss.

1 Like

You call me out on bad scripting habits, but don’t even make variables for something you’re going to use multiple times. I’d appreciate if you’d edit your comment about my code as I went back and tested it, it works fine, the only reason I didn’t take care of the .Touched was due to the fact that a basic SpawnLocation can set a players team value.

Sorry for the incorrect information, I meant tutorials from sites such as YouTube and the Roblox Studio Wiki, or MODELS from the toolbox that can help you get a better understanding of team-changing; for example, the Roblox default free models from their own asset library! :sweat_smile:

just make a team called “No team” and put in your teams folder. that’s all you need to do i think.