You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to make datastore that save teams. -
What is the issue? Include screenshots / videos if possible!
Data store not saving a player team. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
No yet.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local DSS = game:GetService("DataStoreService")
local StageData = DSS:GetDataStore("StageData")
game.Players.PlayerAdded:Connect(function(plr)
local data
local success, err = pcall(function()
data = StageData:GetAsync(plr.UserId)
end)
if success and data then
-- A player has data.
print("Got data.")
plr.Team = data
elseif success and not data then
-- New player.
print("Got no data.")
plr.Team = game:GetService("Teams").Stage1
else
-- Error.
warn("Team data store failed: " .. tostring(err))
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local team = plr.Team
print(team)
local data
local success, err = pcall(function()
data = StageData:GetAsync(plr.UserId)
end)
if success and data then
print("Player has data! (on saving)")
-- Player has data. Updating Data.
StageData:UpdateAsync(plr.UserId, function(oldstage)
local newstage = team
if newstage then
return newstage -- Updated value.
else
newstage = team
if newstage then
return newstage -- Updated value.
else
return nil -- Cancels update because it doesn't contain value.
end
end
end)
elseif success and not data then
print("Player has no data (on saving)")
-- Newbie player leaves. Setting data.
local success, err = pcall(function()
StageData:SetAsync(plr.UserId, team)
end)
if success then
print("Successfully save data player: " .. plr.Name)
else
warn("Failed to save player data! " .. plr.Name .. " error: " .. tostring(err))
end
end
end)