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

Why it won't save cash to DataStore?

Hey, i have problem with DataStore. I think it should works fine, but it says always "no data found"..

Here is script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("CashStore")

game.Players.PlayerAdded:connect(function(player)
	if script.Parent.SettingsTab.CashSave.Value == true then
		pcall(function()
			local store = DataStore:GetAsync("u_"..player.userId)
			if store then
				print("Found cash data")
				PlayerStats.Value = store
			else
				print ("No data found")
			end
		end)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	if script.Parent.SettingsTab.CashSave.Value == true then
		pcall(function()
			DataStore:SetAsync("u_"..PlayerStats.Value)
			print("Saved")
		end)
	end
end)

It's not entire script, because script is long.

1 answer

✓ Accepted answer

In line 20, you only specified the key, not the value. You need to provide the value for it to set as well!

local DataStore = game:GetService("DataStoreService"):GetDataStore("CashStore")

game.Players.PlayerAdded:connect(function(player)
    if script.Parent.SettingsTab.CashSave.Value == true then
        pcall(function()
            local store = DataStore:GetAsync("u_"..player.UserId) -- Also, stop using deprecated stuff! It is a bad habit!
            if store then
                print("Found cash data")
                PlayerStats.Value = store
            else
                print ("No data found")
            end
        end)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    if script.Parent.SettingsTab.CashSave.Value == true then
        pcall(function()
            DataStore:SetAsync("u_"..player.UserId, PlayerStats.Value) -- There, this should work!
            print("Saved")
        end)
    end
end)

Any questions? Please leave a comment below. Thanks!

1 comment

0

Oh.. okay, works fine. Thank you. Anyway that was my first time with DataStores lol

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