Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Datastore not working anymore, can someone explain please?

It was working fine yesterday and days before then, I just tested it today and now it doesn't save anything. It also gives off no errors in the output.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderboardSaveSystem")
local Players = game:GetService("Players")


Players.PlayerAdded:Connect(function(player) 
    local key = tostring(player.UserId) 
    local data = ds:GetAsync(key) 
    local leader = Instance.new("Folder") 
    leader.Name = "leaderstats"
    leader.Parent = player 
    local Speed = Instance.new("NumberValue", leader)
    Speed.Name = "Speed"
    local Rebirth = Instance.new("NumberValue", leader)
    Rebirth.Name = "Rebirth"
	local GemVal = Instance.new("NumberValue", player)
	GemVal.Name = "GemVal"
	local Purchase1 = Instance.new("BoolValue", player)
	Purchase1.Name = "RedTrail"
    local success, msg = pcall(function() 
        if data then  
            Speed.Value = data[1]
            Rebirth.Value = data[2]
			GemVal.Value = data[3]
			Purchase1.Value = data[4]
        else 
            Speed.Value = 0 
            Rebirth.Value = 0
			GemVal.Value = 0
			Purchase1.Value = false
        end
    end)
end) 


Players.PlayerRemoving:Connect(function(player)
    local data = {  
        player.leaderstats.Speed.Value,
        player.leaderstats.Rebirth.Value,
		player.GemVal.Value,
		player.Purchase1.Value
    }

    local key = tostring(player.UserId)
    print("player left:", player)
    ds:SetAsync(key, data)
end)

7 comments

0

Going to mention that it does still provide the previous data that players had before it stopped saving.

0

pretty sure it's a problem at roblox.

0

I thought they fixed that earlier this morning?

0

If it worked before, then just wait for roblox to make a fix if they haven't done so already

0

Alright, I thought I was the only one w/ this issue after the previous "fix".

0

Parent argument to Instance.new is deprecated.

0

what/how else am I supposed to be using it?

1 answer

✓ Accepted answer

Roblox made a function inside the DataModel class called BindToClose(function)

Here's some notes on the Roblox API about this function.

Description: Schedules the given function to be called when the game is about to shut down.

Note: The purpose of this is to give creators a chance to save data using HttpService or Data Store before the game shuts down.

You can find it on Roblox's API page here: https://wiki.roblox.com/index.php?title=API:Class/DataModel/BindToClose

Above this:

Players.PlayerRemoving:Connect(function(player) 

Add this:

game:BindToClose(function()
	wait(3)
	print("Done")
end)

Note: If your Datastore isn't working on Roblox Studio it might just be an issue with their servers and should only be a problem in your Roblox Studio but will work in online mode on their real servers so test that too.

3 comments

0

It happen in both test and real servers, and I tried adding the function in and it hasn't fixed my current issue :(

0

Does your debug console print out on line 45? Looks like you need more debugging for yourself then. My Datastore works but mines also different. Maybe rewrite your script and use pcalls for getasync and setasync for return values

0

It started to work now, I'm just gonna assume that it was on roblox's part?

This discussion is preserved as it was. New answers, comments, and votes are closed.