local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore") game.Players.PlayerAdded:connect(function(player) local team_name = player.Team.Name local team = game.Teams:FindFirstChild(team_name) local key = "user-" .. player.userId local storeditems = datastore:GetAsync(key) if storeditems then team = storeditems[1] print("loaded!") print(team.Name) print(key) else local items = {team} datastore:SetAsync(key, items) error("not loaded!") end end) game.Players.PlayerRemoving:connect(function(player) local items = {player.Team} local key = "user-" .. player.userId datastore:SetAsync(key, items) print("saved! (plr removed)") end)
ERROR MESSAGE: Cannot store Array in data store. Data stores can only accept valid UTF-8 characters. (At the bottom part) Please fix this, i don't know how to get the player's team (aka team (line 5-7))
This is how I would do it.
ModuleScript in ServerScriptService and I named it "TeamModule" -
local teamModule = {} local DataStore2 = require(1936396537) DataStore2.Combine("MainKey","Team") -- You Can Change MainKey To Any New Value To Reset The DataBase local defaultTeam = "None" function teamModule:SetPlayerTeam(player) if player:IsA("Player") then local excessData = Instance.new("Folder",player) excessData.Name = "ExcessData" local teamValue = Instance.new("StringValue",excessData) teamValue.Name = "Team" local teamStore = DataStore2("Team",player) local function updateTeam(team) player.ExcessData.Team.Value = team end updateTeam(teamStore:Get(defaultTeam)) teamStore:OnUpdate(updateTeam) else error("The First Parameter Has To Be A Player") end end return teamModule
This is a ServerScript in ServerScriptService and I named it TeamSet -
local teamModule = require(script.Parent.TeamModule) local DataStore2 = require(1936396537) local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events") game.Players.PlayerAdded:Connect(function(player) teamModule:SetPlayerTeam(player) wait(1) local team = game:GetService("Teams"):FindFirstChild(player.ExcessData.Team.Value).TeamColor wait() player.TeamColor = team wait(0.25) player:LoadCharacter() end) RE.TeamChange.OnServerEvent:Connect(function(player,team) player.TeamColor = game:GetService("Teams"):FindFirstChild(team).TeamColor player.ExcessData.Team.Value = team local teamStore = DataStore2("Team",player) teamStore:Set(team) wait(0.1) player:LoadCharacter() end)
And Then I have a folder in ReplicatedStorage called Events, and you should keep all RemoteEvents and Functions in there, and it will save their team. All you have to do is have the first team enabled Auto-Assignable, and the other teams should not have that enabled. It will get their team, and then respawn them on their team at that spawn. It should work, if it doesn't, let me know so I can tweak it and fix it for you. And for it to work in studio if you want to test it, you have to put a BoolValue inside of ServerStorage and name it SaveInStudio and have it enabled to true for it to work in studio.