What do you want to achieve?
I want to create a script for leaderstats, where there is two values. I wanna save the money’s value in a DataStore when the player leaves, and retrieve it when the player joins.
What is the issue? I don’t get any warnings/errors in my script, but the issue is that the money’s value always stays 0, even if it’s says “Successfully saved” and “Success got!”.
What solutions have you tried so far? I’ve tried to use different similar scripts, but it’s not working at all. Same thing happens, no error and no saving.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerStats")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
local data
local success, errorMsg = pcall(function()
data = DataStore:GetAsync("Player_"..player.UserId)
end)
if success then
print("Success got!")
money.Value = data
print(data)
else
warn(errorMsg)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
if plr then
local e = plr.leaderstats.Money.Value
local success, errorMsg = pcall(function()
DataStore:SetAsync("Player_"..plr.UserId, e)
end)
if success then
print("Successfully saved")
print(DataStore)
else
warn(errorMsg)
end
end
end)
Could someone please help me? By the way, I have both HTTP service and Access API enabled.
Are you modifying the money value locally? This looks like something that should work, so I’m assuming that the issue is that the server does not see the money change.
local moneyData = game:GetService("DataStoreService"):GetDataStore("MoneyData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
local success, errorMessage = pcall(function()
-- also you could use not "Player_"..userId, you can use just player.UserId, cause it's converts into string
money.Value = moneyData:GetAsync(player.UserId) or 0 -- if data isn't found value could return to 0
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
moneyData:SetAsync(player.UserId, player.leaderstats.Money.Value)
end)
end)
you can see, this is a easier and cleaner script as your’s
check it
Not that one. The script that modifies the money itself. Even if the script is in the server, if you were to just change the money.Value from the client, it wouldn’t reflect over the server. Try doing a test by purposely adding 10 to the money value after loading them in (so right before the PlayerAdded function is about to end), and then leave the game. See if that’ll work.