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

DataStore not working?

Alright, so I have been making a game recently and need to try to save a stat called BankedMoney located under the Player it is not in a leaderboard* it's a GUI. here is the code that I am trying to use if you could look over it and tell me where I went wrong that would be great!

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

print("Money Leaderboard Working")

game.Players.PlayerAdded:connect(function(player)
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = player
	
	local BankedMoney = Instance.new("IntValue")
	BankedMoney.Name = "BankedMoney"
	BankedMoney.Parent = player
		
	local Key = "player-"..player.userid
	
	local savedvalues = DataStore:getasync(Key) 
	
	if savedvalues then
		BankedMoney.Value = savedvalues[1]
	  else
		local valuestosave = {BankedMoney.value}
		DataStore:setasync(Key, valuestosave)
	end
		
end)

I also have a player leaving script for the DataStore

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

game.Players.PlayerRemoving:connect(function(player)

local Key = "player-"..player.userid

local valuestosave = {player.BankedMoney.Value}
DataStore:setasync(Key, valuestosave)
		
end)

2 answers

✓ Accepted answer

The problem with your code is userid should be UserId Enter:

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

print("Money Leaderboard Working")

game.Players.PlayerAdded:connect(function(player)
    local Money = Instance.new("IntValue")
    Money.Name = "Money"
    Money.Parent = player

    local BankedMoney = Instance.new("IntValue")
    BankedMoney.Name = "BankedMoney"
    BankedMoney.Parent = player

    local Key = "player-"..player.UserId

    local savedvalues = DataStore:getasync(Key) 

    if savedvalues then
        BankedMoney.Value = savedvalues[1]
      else
        local valuestosave = {BankedMoney.value}
        DataStore:setasync(Key, valuestosave)
    end

end)

Leaving:

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

game.Players.PlayerRemoving:connect(function(player)

local Key = "player-"..player.UserId

local valuestosave = {player.BankedMoney.Value}
DataStore:setasync(Key, valuestosave)

end)

1 comment

0

@MrLonely1221 still won't work

In my opinion, I think there are some capitalization problems. Like try typing :GetAsync() instead of :getasync() since it would be read as one word, and GetAsync are two separate words.

Here! I fixed the capital issues for you.

Main Script:

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

print("Money Leaderboard Working")

game.Players.PlayerAdded:connect(function(player)
    local Money = Instance.new("IntValue")
    Money.Name = "Money"
    Money.Parent = player

    local BankedMoney = Instance.new("IntValue")
    BankedMoney.Name = "BankedMoney"
    BankedMoney.Parent = player

    local Key = "player-"..player.UserId

    local savedvalues = DataStore:GetAsync(Key)  -- Changed.

    if savedvalues then
        BankedMoney.Value = savedvalues[1]
      else
        local valuestosave = {BankedMoney.value}
        DataStore:SetAsync(Key, valuestosave) -- Changed.
    end

end)

Leaving Script:

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

game.Players.PlayerRemoving:connect(function(player)

local Key = "player-"..player.UserId

local valuestosave = {player.BankedMoney.Value}
DataStore:SetAsync(Key, valuestosave) -- Changed.

end)


Next time, NEVER forget to separate words using capital letters at the start of each word (without space in scripts, of course)!!!

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