Hi Im trying to make a money value save but I am stuck rn
I keep getting this error:
This is the script:
local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local key = "user_"..plr.UserId
local money = plr.PlayerGui.Money.TextLabel.Money
local GetSaved = ds:GetAsync(key)
if GetSaved then
money.Value = GetSaved
else
local money2 = money.Value
print(money2)
ds:GetAsync(key, money2)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local savevalue1 = plr.PlayerGui.Money.TextLabel.Money
ds:SetAsync("user_"..plr.UserId, {savevalue1.Value})
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetChildren()) do
plr:Kick()
end
end)
local GetSaved = ds:GetAsync(key)
if GetSaved then
money.Value = GetSaved
else
local money2 = money.Value
print(money2)
ds:GetAsync(key, money2)
end
The problem is at line ds:GetAsync(key, money2) your trying to pass “money2” as a argument when getasync only requires 1.
Im not sure why your calling getasync here but you can try this code
local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local key = "user_"..plr.UserId
local money = plr.PlayerGui.Money.TextLabel.Money
local sucess, warnmsg = pcall(function()
local GetSaved = ds:GetAsync(key)
if GetSaved then
money.Value = GetSaved
else
money.Value = 10 --some default value since player has no data
end
end)
if warnmsg then
warn(warnmsg)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local savevalue1 = plr.PlayerGui.Money.TextLabel.Money
local sucess, warnmsg = pcall(function()
ds:SetAsync("user_"..plr.UserId, savevalue1.Value)
end)
if warnmsg then
warn(warnmsg)
end
end)
--removed the game bind to close because its unnecessary
I would also suggest doing waitforchild on playergui since it may not be loaded.
Add a print statement after it saves to see if it was successful
local sucess, warnmsg = pcall(function()
ds:SetAsync("user_"..plr.UserId, savevalue1.Value)
end)
if sucess then
print("saved data")
print(savevalue1.Value)
else
warn(warnmsg)
end
If it isn’t printing anything, then use BindToClose to save the players data.
game:BindToClose(function()
task.wait(5) --You can also cycle through every player in the game and save their data, but task.wait(5) should work for now.
end)
Can you post the updated version of the script in case you made a typo somewhere? It is strange that the playerremoving block isn’t running at all.
By the way, this isn’t related to the issue but saving a player’s money inside of their Gui is a bad idea because any exploiter can just change their money value to whatever they want. Put their money value in the server or under their player’s instance (the one in game.Players, not in workspace)
now its back to not printing anything again while not working
heres the full script you asked for:
local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local key = "user_"..plr.UserId
local money = plr:WaitForChild("PlayerGui"):WaitForChild("Money").TextLabel.Money
local sucess, warnmsg = pcall(function()
local GetSaved = ds:GetAsync(key)
if GetSaved then
money.Value = GetSaved
else
money.Value = 10 --some default value since player has no data
end
end)
if warnmsg then
warn(warnmsg)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local money2 = plr:WaitForChild("PlayerGui"):WaitForChild("Money").TextLabel.Money
local sucess, warnmsg = pcall(function()
ds:SetAsync("user_"..plr.UserId, money2.Value)
end)
if sucess then
print("saved data")
print(money2.Value)
else
warn(warnmsg)
end
end)
--removed the game bind to close because its unnecessary