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)
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)
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)!!!