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

Why is DataStore not saving properly?

Hey, I've been messing around with DataStore recently and for some reason when i was testing it, it wouldn't save. I did some investigating and the loading was fine, it just wouldn't save. There are no error messages, I've tried this in the game and in studio, i do have studio API enabled.

local DataStoreService = game:GetService("DataStoreService")

local TestStore = DataStoreService:GetDataStore("TestStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local TestVariable = Instance.new("IntValue", leaderstats)
	TestVariable.Name = "Test"
	TestVariable.Value = TestStore:GetAsync(player.UserId)
end)

game.Players.PlayerRemoving:Connect(function(player)
	TestStore:SetAsync(player.UserId,player.leaderstats.Test.Value)
end)

1 answer

This works fine for me. Try putting the script in the ServerScriptStorage if you haven't.

Also, try the following as it makes sure the player is loaded before leaderstats.

local DataStoreService = game:GetService("DataStoreService")

local TestStore = DataStoreService:GetDataStore("TestStore")

game.Players.PlayerAdded:Connect(function(player)
    wait(1)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local TestVariable = Instance.new("IntValue", leaderstats)
    TestVariable.Name = "Test"
    TestVariable.Value = TestStore:GetAsync(player.UserId)
end)

game.Players.PlayerRemoving:Connect(function(player)
    TestStore:SetAsync(player.UserId,player.leaderstats.Test.Value)
end)

3 comments

0

That is really strange. Still not getting anything. I've tried adding pcalls and bindtoclose but still nothing. Any ideas?

0

Are you using this is inside a serverscript instead of a localscript?

0

yes it is in a server script

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