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

Why does this Datastore2 not save my data?

It's a script in Serverscriptstorage and yeah, it doesn't save. No errors. Why does it not save?

local Datastore2 = require(script.Parent.MainModule)

Datastore2.Combine("MainKey", "Coins", "Gems")

game.Players.PlayerAdded:Connect(function(plr)
	local dataCoins = Datastore2("Coins", plr)
	local dataGems = Datastore2("Gems", plr)
	
	local folder = Instance.new("Folder", plr)
	folder.Name = "Stats"
	
	local coins = Instance.new("IntValue", folder)
	local gems = Instance.new("IntValue", folder)
	coins.Name = "Coins"
	gems.Name = "Gems"
	
	if dataCoins:Get() == nil then
		coins.Value = dataCoins:Get()
		print(coins.Value)
	else
		coins.Value = 0
	end
	if dataGems:Get() == nil then
		gems.Value = dataGems:Get()
		print(gems.Value)
	else
		gems.Value = 0
	end
	coins.Changed:Connect(function()
	dataCoins:Set(coins.Value)
	end)
	gems.Changed:Connect(function()
	dataGems:Set(gems.Value)
	end)
end)

Thanks in advance!

2 comments

0

Is "Enable Studio Access to Api Services" enabled?

0

And have you made a boolean value in ServerStorage called "SaveInStudio" and made it true?

1 answer

✓ Accepted answer

From what it looks like, you are trying to fetch data that a player already has, but you are doing it in reverse. What your script is doing is searching to see if the player does have data, and if they do, then you are setting it to 0.

Simple fix. All you need to do is change the

if dataCoins:Get() == nil then

to

if dataCoins:Get() ~= nil then

~= is a symbol in roblox lua for not equal to

You did the same thing for 'dataGems'

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