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

Datastores not working, not saving my leaderstat value. How do i fix this?

****so i have this script and i tried changing the value of the leaderstats R-Bucks in game, and i did. But when i rejoined it reset me to the default 50 R-Bucks.

local currencyName = "R-Bucks"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
end)

if savedData ~= nil then
	currency.Value = savedData
	print("data has been loaded")
	else
		currency.Value = 50
		print("New player has entered")
	end


end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("Server being shutdown.")
		end
	end
	wait(5)
end)
``

1 answer

Try this: I edited your script: To learn more about DataStores and how they work visit: https://developer.roblox.com/en-us/api-reference/class/DataStoreService


-- Dont forget to accept my answer on scripting helpers
local currencyName = "R-Bucks"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)

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

    local currency = Instance.new("IntValue")
    currency.Name = currencyName
    currency.Parent = folder
    currency.Value = 0
    local ID = currencyName.."-"..player.UserId
    local savedData = nil

    pcall(function()
        savedData = DataStore:GetAsync(ID)
end)

if savedData ~= nil then
	game.Players.PlayerRemoving:Connect(function(player)
    local ID = currencyName.."-"..player.UserId
    DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
    currency.Value = currency.Value + 0
    print("data has been loaded")
    else
        currency.Value = 999 -- This is the amount each players is going to get when they enter the server.
        print("New player has entered")
    end


end)

game.Players.PlayerRemoving:Connect(function(player)
    local ID = currencyName.."-"..player.UserId
    DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()

    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick("Server being shutdown.")
        end
    end
    wait(5)
end)

1 comment

0

i played in studio with that script, set the value to 999, rejoined and it was 0, so it didnt even set me to default value of 50

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