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

Why does my DataStore NEVER save like everyone else's?

I'm pretty sure this script is outdated, but here it is.(ServerScript in ServerScriptService)

local DS = game:GetService("DataStoreService")
local VertigoVal = DS:GetDataStore("VertigoVal")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	local Vertigo = Instance.new("IntValue",leaderstats)
	Vertigo.Name = "Vertigo"
	Vertigo.Value = 100
	

	game.Players.PlayerRemoving:Connect(function(plr)
		VertigoVal:SetAsync(plr.UserId,Vertigo.Value)
	end)
	Vertigo.Value = VertigoVal:GetAsync(plr.UserId) or 100
	while true do
		wait(30)
		VertigoVal:SetAsync(plr.UserId,Vertigo.Value)
	end
end)

When I test the game, I manually set my Vertigo (cash) to a random number, not 100. But when I re-test, the value doesn't save. I turned on API services and HTTP services.

What on earth is going on? Is it because it's outdated?

Please help!

2 answers

Here is what I got:

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("MoneyStats") -- Change this with a random data name!
 
game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local currency = Instance.new("IntValue", folder)
    currency.Name = "Vertigo" -- Currency name.
    currency.Value = ds:GetAsync(plr.UserId) or 100
    currency.Changed:Connect(function()
        ds:SetAsync(plr.UserId, currency.Value)
    end)
end)
 
game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync(plr.UserId, plr.leaderstats.Vertigo.Value)
end)

This always works for me... If it does not work for you Then your game may be a bit broken.

PS Script type : Script Property : ServerScriptService

7 comments

0

Oh and yeah, I forgot to tell you That THERES no wait time!

0

No.. It doesn't work. How could my game be broken?

0

Try the script and Turn API services on

0

I did.

0

Is it in ServerScriptService and the script type is Script?

0

Yes.

0

Hmm...

There isn't anything directly wrong with the script, but it can be improved.

Functions like SetAsync() that access a data store's contents are network calls that may occasionally fail. As shown above, it's recommended that these calls be wrapped in pcall() to catch and handle errors.

Note: SetAsync() can be hazardous since it overwrites any value currently in the entry. If you're updating an existing entry, UpdateAsync() is recommended because it considers the old value before making changes.

Source: https://developer.roblox.com/en-us/articles/Data-store

And lastly:

  1. you should not use the Instance.new() with a parent argument if you are assigning properties afterwards as that will be slower.

  2. You should not update (SetAsync/UpdateAsync) its value everytime it has been changed as that will lead to datastore throttles (learn more about it here). The only ones you need are a PlayerAdded, PlayerRemoved, and a BindToClose event, and perhaps a interval that updates their data every x minutes.

2 comments

0

Nope! Doesn't work. I still don't know why it's only me, not anyone else! Please give me more explanation, rather than copy-pasting off of devforum.

0

You should look into pcall(). Source: https://www.lua.org/pil/8.4.html

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