#Datastore doesnt store
1 messages · Page 1 of 1 (latest)
You might have forgotten to turn on api services from game settings
I went to game settings but there was only for studio
well you prob changed your datastore name or someth. I can't know what's wrong without ur code
Should i send u the code?
send it here
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local dataStoreName = RunService:IsStudio() and "PlayerPointsData_Studio" or "PlayerPointsData"
local PointsDataStore = DataStoreService:GetDataStore(dataStoreName)
local autoSaveInterval = 10 -- Save every 30 seconds
local function loadPlayerData(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
local success, data = pcall(function()
return PointsDataStore:GetAsync(player.UserId)
end)
if success and typeof(data) == "number" then
points.Value = data
else
points.Value = 0
end
end
local function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
local points = leaderstats and leaderstats:FindFirstChild("Points")
if points then
pcall(function()
PointsDataStore:UpdateAsync(player.UserId, function()
return points.Value
end)
end)
end
end
task.spawn(function()
while true do
task.wait(autoSaveInterval)
for _, player in pairs(Players:GetPlayers()) do
savePlayerData(player)
end
end
end)
Players.PlayerAdded:Connect(loadPlayerData)
Players.PlayerRemoving:Connect(savePlayerData)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
savePlayerData(player)
end
end)
this is the script for Datastore i made
local coinModel = script.Parent
local hitbox = coinModel:FindFirstChild("Hitbox_1") -- or "Hitbox"
local pointsAmount = 4
local collected = false
local connection
local function onTouch(otherPart)
if collected then return end
collected = true
local character = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if not player or not character:FindFirstChild("Humanoid") then
collected = false
return
end
local leaderstats = player:FindFirstChild("leaderstats")
local points = leaderstats and leaderstats:FindFirstChild("Points")
if points then
points.Value += pointsAmount
end
for _, obj in pairs(coinModel:GetDescendants()) do
if obj:IsA("BasePart") then
obj.Anchored = true
obj.CanCollide = false
obj.Transparency = 1
end
end
if connection then
connection:Disconnect()
end
task.delay(0.5, function()
if coinModel then
coinModel:Destroy()
end
end)
end
if hitbox then
connection = hitbox.Touched:Connect(onTouch)
else
warn("No Hitbox_1 found in the coin model.")
end
this is the script for the coin
.
:/
is that a server script or localscript?
anything in your output window?
nope nothing
could you re-state the problem? only things i see possible of resetting data here is the fact of using 2 different datastores depending on studio local dataStoreName = RunService:IsStudio() and "PlayerPointsData_Studio" or "PlayerPointsData" or the getasync is failing lua if success and typeof(data) == "number" then points.Value = data else points.Value = 0 end
your autosave interval is also extremely low local autoSaveInterval = 10 -- Save every 30 seconds but i dont think this would go over the limits on its own
if you had multiple datastores then it absolutely would
publishing doesnt reset data in the datastore
idk
wanna come in my game and see?
cuz when u pick up the coins and leave and join again it just stores fine
thanks for confirming that, is why i asked you to re-state the problem
the fact still stands though your resets are only coming from one of 2 places
so there is no problem in the code?
there may be a 3rd very rare very unlikely edgecase however, where the save function lua task.spawn(function() while true do task.wait(autoSaveInterval) for , player in pairs(Players:GetPlayers()) do savePlayerData(player) end end end)
executes before the load function finishes executing.
this stands to reason where you parent the leaderstats before loading the data into it.
quickfix for this edge case would be to parent the leaderstats folder to the player after the datastore operations and not before, like it should be the last thing the loadplayer function should do. this will block this edge case because it wont be possible for local points = leaderstats and leaderstats:FindFirstChild("Points") to exist before the loading has fully completed.
bruh i just explained you don't handle the case where the datastore:getasync fails. which can 100% happen.
maybe i wasn't super clear on that ;o
my brain boom
** You are now Level 6! **
i would've suspected you're flooding the requests, but at a rate of 6 operations per minute you should be good
so what should i do with the code
datastore limits and possible errors found here https://create.roblox.com/docs/en-us/cloud-services/data-stores/error-codes-and-limits
well firstly you should have something to catch for errors, normally if you cannot reasonably load data you can just kick the player with a message saying to retry
most people add retries as well so if it can't load within a few attempts then it'll kick
do be sure to check success, not data, since new players will have data=nil on first load
this is the second thing i would do
and 3rd, i'd suggest adding some debugging in case it comes up again.
like just a few prints like load successful, along with the actual data, may help. i take it you're testing this in the roblox player, so warns may be better since those will show up in the live error report on the game's dashboard.
oh for the output
just while you're working on it y'know
ok ill do ur steps then i comeback later
but ye nothing to do with actual publishing since that cannot reset datastores. it must be something with your code, or you may have other code messing with this, like you accidentally have 2 copies of the script or something.