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

Can someone help me with this simple DataStore script?

It's a simple script. but I'm new to DataStore so I'm not sure if I'm doing it right. When I check to see if it saved the cash, it loads a lower number and not the number of money I had before. So say I had 1400, but when I rejoined I only had 900. How would I make this better?

StoreCash = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEnter(player)	
	local board = Instance.new("IntValue",player)
	board.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue",board)
	Cash.Name = "Money"
	
	local key = "user_" .. player.userId
	if StoreCash:GetAsync (key) ~= nil then
		Cash.Value = StoreCash:GetAsync (key)
	else 
		Cash.Value = 500
	end
	Cash.Changed:connect(function(val)
		StoreCash:SetAsync (key, Cash.Value)
	end)
end

game.Players.PlayerAdded:connect(PlayerEnter)

1 answer

I think you should move

    Cash.Changed:connect(function(val)
        StoreCash:SetAsync (key, Cash.Value)
    end)

outside PlayerEnter function.

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