Why doesnt my leaderstats work?

help

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyTest")
------------------get the dataStore service and name ^^^ -----------------------


game.Players.PlayerAdded:Connect(function(player)
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    local cash = Instance.new("IntValue")
    cash.Name = "Money"
    cash.Parent = leaderstats    

    local data
    local success, errormessage = pcall(function()
        
        data = DataStore:GetAsync(player.UserId.."-Money")

    end)
    
    if success then
        cash.Value = data
        print("wow its working")
    else
        print("Ur datastore not working buddy")
        warn(errormessage.."Ur bad kid")
    end
    
    
end)


game.Players.PlayerRemoving:Connect(function(plr)
    
    local success, errormessage = pcall(function()
        
        DataStore:GetAsync(plr.UserId.."-Money", plr.leaderstats.Money)
    end)
    
    if success then
        print("it saved bucko")
    else
        print("ur still bad")
        warn(errormessage.."keep urself safe bud :D")
    end
    
    
    
end)

help, when i get money it works, but when i leave it doesn’t save

In your PlayerRemoving code, you are using :GetAsync when you should be using :SetAsync()

1 Like
DataStore:SetAsync(plr.UserId.."-Money", plr.leaderstats.Money.Value)

You forgot the value also

(Wrote in Mobile)

Hare fix script. like him @Cxdious or him @SeargentAUS or me if you whant… @ptsd0turtle just copy and pasta

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyTest")
------------------get the dataStore service and name ^^^ -----------------------


game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Money"
	cash.Parent = leaderstats    

	local data
	local success, errormessage = pcall(function()

		data = DataStore:GetAsync(player.UserId.."-Money")

	end)

	if success then
		cash.Value = data
		print("wow its working")
	else
		print("Ur datastore not working buddy")
		warn(errormessage.."Ur bad kid")
	end


end)


game.Players.PlayerRemoving:Connect(function(plr)

	local success, errormessage = pcall(function()

		DataStore:SetAsync(plr.UserId.."-Money", plr.leaderstats.Money.Value)
	end)

	if success then
		print("it saved bucko")
	else
		print("ur still bad")
		warn(errormessage.."keep urself safe bud :D")
	end



end)

[/quote]

Wouldn’t it be Money.Value? because he’s trying to save the money value or is it a new way to save data?