How would I make a datastore for like each team the player is on?
For example, when a player first plays and joins as Team 1 all the progress will save on that team or profile. If the player comes back and joins as Team 2 the progress would be different.
I am not really sure how I can make this but in simple words I am trying to make a Team based datastore.
local DataStoreService = game:GetService("DataStoreService")
local TeamService = game:GetService("Teams")
local Teams = TeamService:GetTeams()
--// getting data at team creation
local function getTeamData(team)
local teamDataStore = DataStoreService:GetDataStore(team.Name)
-- insert preferred method of getting data here
end
TeamService.ChildAdded:Connect(function(team)
if not table.find(Teams, team) then
table.insert(Teams, team)
end
local success, result = pcall(function()
return getTeamData(team)
end
if not success then
-- failed to get, insert preferred method of handling here
end
end)
--// saving data at the end
local function saveTeamData(team)
local teamDataStore = DataStoreService:GetDataStore(team.Name)
-- insert preferred method of saving data here
end
for _, team in ipairs(Teams) do
local success, result = pcall(function()
saveTeamData(team)
end)
if not success then
-- failed to save, insert preferred method of handling this here
end
end
Be sure to use a table when you’re saving data, and don’t save data every time there’s a change. Use a table to act as the cache instead, just as you would do when saving player data.
Hmmm well I already have a datastore I just dont know I would… well change it?
local Datastore = game:GetService("DataStoreService"):GetDataStore("EpicReallyMa1n21")
local PlayerPositions = {}
function PlayerAdded(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Data = Datastore:GetAsync(Player.UserId) or {Pounds = 1000, Inventory = {"Brownbess Musket","Lantern","ClassicSword","ClassicSword","ClassicSword"},BankInventory = {}, BankPounds = 0, Position = nil}
local Cash = Instance.new("IntValue")
Cash.Name = "Pounds"
Cash.Value = Data.Pounds
Cash.Parent = leaderstats
local InventoryFolder = Instance.new("Folder")
InventoryFolder.Name = "Inventory"
local BankData = Instance.new("Folder")
BankData.Name = "BankData"
local Items = Instance.new("Folder")
Items.Name = "Items"
Items.Parent = BankData
local Cash = Instance.new("IntValue")
Cash.Name = "Pounds"
Cash.Value = Data.BankPounds
Cash.Parent = BankData
for i,v in pairs(Data.Inventory) do
local Item = Instance.new("ObjectValue")
Item.Name = v
Item.Value = game:GetService("ReplicatedStorage").Items[v]
Item.Parent = InventoryFolder
end
for i,v in pairs(Data.BankInventory) do
local Item = Instance.new("ObjectValue")
Item.Name = v
Item.Value = game:GetService("ReplicatedStorage").Items[v]
Item.Parent = Items
end
BankData.Parent = Player
InventoryFolder.Parent = Player
leaderstats.Parent = Player
local char = Player.Character or Player.CharacterAdded:Wait()
if Data.Position then
char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(unpack(Data.Position))
end
local function CharacterAdded(character)
local v = game:GetService("RunService").Stepped:Connect(function()
PlayerPositions[Player.Name] = character:WaitForChild("HumanoidRootPart").Position
end)
Player.CharacterRemoving:Connect(function(char)
v:Disconnect()
end)
end
CharacterAdded(char)
Player.CharacterAdded:Connect(CharacterAdded)
end
function PlayerRemoving(Player)
Datastore:UpdateAsync(Player.UserId,function(oldval)
local Data = {}
Data.Pounds = Player.leaderstats.Pounds.Value
Data.Inventory = {}
for i,v in pairs(Player.Inventory:GetChildren()) do
table.insert(Data.Inventory,1,v.Name)
end
Data.BankPounds = Player.BankData.Pounds.Value
Data.BankInventory = {}
for i,v in pairs(Player.BankData.Items:GetChildren()) do
table.insert(Data.BankInventory,1,v.Name)
end
Data.Position = {PlayerPositions[Player.Name].X,PlayerPositions[Player.Name].Y,PlayerPositions[Player.Name].Z}
return Data
end)
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)