You’ll need to follow these steps carefully:
- Make sure that the place you’re using/testing the DataStore is published to Roblox. You can’t use/test DataStores in unpublished places like local files
- Go to the Home tab, then click on the Game Settings. When the popup opens, go to Security then make sure you have “Enable Studio Access to API Services” turned on (the switch is colored green when on)
- Finally after making sure you’ve followed the steps above correctly, I made this script you can use for your DataStore:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
Players.PlayerAdded:Connect(function(player)
local success, values = pcall(myDataStore.GetAsync, myDataStore, "Player"..player.UserId)
if success then
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Value = values.Cash
Cash.Parent = leaderstats
local Diamonds = Instance.new("IntValue")
Diamonds.Name = "Diamonds"
Diamonds.Value = values.Diamonds
Diamonds.Parent = leaderstats
while true do
Cash.Value += 1
Diamonds.Value += 1
task.wait()
end
else
warn(values)
end
end)
local function saveData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local success, result = pcall(myDataStore.SetAsync, myDataStore, "Player"..player.UserId, {
Cash = leaderstats.Cash.Value,
Diamonds = leaderstats.Diamonds.Value
})
if not success then warn(result) end
end
end
Players.PlayerRemoving:Connect(saveData)
if RunService:IsStudio() then
game:BindToClose(function()
task.wait(4)
end)
else
game:BindToClose(function()
local x, y = 0, 0
for _, player in Players:GetPlayers() do
x += 1
coroutine.wrap(function()
saveData(player)
y += 1
end)()
end
repeat task.wait() until y == x
end)
end