I want to access the same datastore 2 from different scripts. I have no idea where to start and I believe there aren’t any posts about this.
You just get the datastore like you normally would I think :GetDataStore(datastorename)
2 Likes
can I have two examples scripts? Thanks!
-- saving data script
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("PlayerDataStore")
local Players = game:GetService("Players")
function SavePlayerData(player)
local key = "PlayerData_" .. player.UserId
local dataToSave = {
Coins = 1000,
Level = 5,
}
myDataStore:SetAsync(key, dataToSave)
end
Players.PlayerRemoving:Connect(function(player)
SavePlayerData(player)
end)
-- loading data script
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("PlayerDataStore")
local Players = game:GetService("Players")
function LoadPlayerData(player)
local key = "PlayerData_" .. player.UserId
local savedData = myDataStore:GetAsync(key)
if savedData then
-- use the loaded data to set player properties, inventory, etc.
player.leaderstats.Coins.Value = savedData.Coins
player.leaderstats.Level.Value = savedData.Level
else
player.leaderstats.Coins.Value = 0
player.leaderstats.Level.Value = 1
-- Set other default properties as needed
end
end
Players.PlayerAdded:Connect(function(player)
LoadPlayerData(player)
end)
You can also use datastores in modules too
1 Like
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.