Basically I want to keep my data when I teleport to each place in my game!
For example:
Place 1 (Lobby) I have 50 coins, 1 gun, 1 custom character, Daily rewards (if collected on one of the places it will show same on all places) and other items store with the character.
Place 2 (Story game) I have the exact same things from the first place.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local playerData = DataStoreService:GetDataStore("PlayerData")
Players.PlayerAdded:Connect(function(player)
local success, data = pcall(function()
return playerData:GetAsync(player.UserId) --Yields, may return nil
end)
if success then
--If they've never joined before
if data == nil then
data = {
coins = 0,
inventory = {},
lastClaimedReward = 0,
}
end
--Set their coins, give them their inventory, et cetera
else
--[[Probably best to retry getting their data, but for this example we will kick them
to prevent overriding their data]]
warn(`Unable to load {player.Name}'s data.`)
player:Kick("Unable to load your data, please try again later.")
end
end)
Players.PlayerRemoving:Connect(function(player)
--Save their data when they leave
end)