local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local player = game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local collectibles = Instance.new("IntValue")
collectibles.Name = "Collectibles"
collectibles.Parent = leaderstats
collectibles.Value = workspace.Collectibles.Value
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = 1
player.RespawnLocation = workspace.Level1.L1Spawn
local playerUserID = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserID)
end)
if success then
collectibles.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function()
local playerUserID = "Player_"..player.UserId
local data = player.leaderstats.collectibles.Value
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserID, data)
end)
if success then
print("data saved")
else
print("data not saved")
warn(errormessage)
end
end)
COMMUNITY QUESTION
datastore is not saving data?
2 answers
I fixed the problem, was just some minor capitalization errors on my part.
First of all Remember to turn API Services on, so you need to publish the game and go to game settings and turn API Services on
Second of all it does not work in roblox studio
Third of all your good to go!
3 comments
0
DataStore very much does work in studio.
0
it doesn't seem to be working in game either
0
it works in studio.
This discussion is preserved as it was. New answers, comments, and votes are closed.

10 comments
Are there any errors in the output, and if so, what line is throwing the error?
no errors
Also, could you explain your problem in more detail instead of saying it doesn't work and posting your script?
the value isnt saving
remove the local player = on line 4 as it is pointless
i tried again and got this error "UserId is not a valid member of RBXScriptConnection"
How are you testing whether or not it saves? If you play the game and manually edit your intvalue, it won't save. However, if you write up a script that changes to intvalue, it does save.
i have a collectible coin that I tried picking up
Also, you should add "player" as a parameter for the leave function
As akatak_prime said add in the player paramater as your code will try and read the varable on line 4. Lua will take the variable that has the closest scope if multiple variable with the same name exist.