#Datastore doesnt store

1 messages · Page 1 of 1 (latest)

tacit crane
#

I made a datastore when players collect a coin it saves to datastore and the points show up in the leaderboard. However when i publish it again the Points disapear.

errant sapphire
tiny vessel
tacit crane
#

I went to game settings but there was only for studio

tiny vessel
#

game settings --> Security --> Enable Studio Access to API Services

#

is this on?

tiny vessel
#

well you prob changed your datastore name or someth. I can't know what's wrong without ur code

tacit crane
#

Should i send u the code?

tiny vessel
#

send it here

tacit crane
#

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

tacit crane
tacit crane
#

:/

errant sapphire
tacit crane
#

server

#

the datastore works but when i publish it again from studio it resets

errant sapphire
#

anything in your output window?

tacit crane
#

nope nothing

errant sapphire
#

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

tacit crane
#

bro datastore works fine

#

only when i publish it it resets

errant sapphire
tacit crane
#

idk

#

wanna come in my game and see?

#

cuz when u pick up the coins and leave and join again it just stores fine

errant sapphire
#

the fact still stands though your resets are only coming from one of 2 places

tacit crane
#

so there is no problem in the code?

errant sapphire
#

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.

errant sapphire
#

maybe i wasn't super clear on that ;o

tacit crane
#

my brain boom

tall gyroBOT
#

studio** You are now Level 6! **studio

errant sapphire
tacit crane
#

so what should i do with the code

errant sapphire
errant sapphire
#

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

errant sapphire
#

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.

tacit crane
#

oh for the output

errant sapphire
#

just while you're working on it y'know

tacit crane
#

ok ill do ur steps then i comeback later

errant sapphire
#

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.