Datastore removes leaderstats bug

i mean on datastores not scripting as a hole

ill try again later, just not motivated rn

Did they even get created? Are you indexing them before they even exist? Do they get created at all or your code just yields? Etc…

local leaderstats = Instance.new(“Folder”, player)
leaderstats.Name = “leaderstats”

local Food = Instance.new("IntValue", leaderstats)
Food.Value = 0

local Coins = Instance.new("IntValue", leaderstats)
Coins.Value = 0

same server script, at the top

I will go now, but if you still don’t find the issue, i will help you in some hours cause no pc right now

Try debugging as much as you can in the meantime

1 Like

ok
also in the future if you need it to help me heres the tutorial i followed:

1 Like

You should try this and see if it works

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local CurrencyGui = script.Parent
local CurrencyHolderFrame = CurrencyGui:FindFirstChild("CurrencyHolder")
local Food = CurrencyHolderFrame:FindFirstChild("Food")
local Coins = CurrencyHolderFrame:FindFirstChild("Coins")

local function changeValue(name: string, StatsDisplay: number)
	if name == "Food" then
		Food.Frame.StatsDisplay.Text = StatsDisplay.."/30"
	elseif name == "Coins" then
		Coins.Frame.StatsDisplay.Text = StatsDisplay
	end
	
end

repeat task.wait() until player:FindFirstChild("leaderstats") 

local leaderstats = player:WaitForChild("leaderstats")

local Food = leaderstats:WaitForChild("Food")
local Coins = leaderstats:WaitForChild("Coins")

changeValue("Food", Food.Value)
changeValue("Coins", Coins.Value)

Food.Changed:Connect(function()
	changeValue("Food", Food.Value)
end)
Coins.Changed:Connect(function()
changeValue("Coins",Coins.Value)
end)
2 Likes

why do you even do a repeat until if you just called waitforchild?

1 Like

LocalScript in StarterPlayerScripts

--Local Script in StarterPlayerScripts
local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

local CurrencyGui = playerGui.CurrencyGui --Change this to your Currency Gui name
local CurrencyHolderFrame = CurrencyGui.CurrencyHolder --Also change this if you need
local leaderstats = player:WaitForChild("leaderstats")

local valueBaseNames = {
	["Food"] = CurrencyHolderFrame.Food.Frame.StatsDisplay,
	["Coins"] = CurrencyHolderFrame.Coins.Frame.StatsDisplay,
}

local toAppend = {
	["Food"] = "/30",
	["Coins"] = "", --Will not append anything, basically
}

function ChangeValue(name, stat)
	if name ~= nil and valueBaseNames[name] then
		(valueBaseNames[name]).Text = stat..toAppend[name]
	end
end

function Start()
	for i, v in pairs(leaderstats:GetChildren()) do
		if v:IsA("ValueBase") then
			ChangeValue(v.Name, v.Value)
			--
			v.Changed:Connect(function()
				ChangeValue(v.Name, v.Value)
			end)
		end
	end
end
Start()
1 Like

now for the server script you should dm me when you are active so i can explain you

1 Like

I made some changes to your datastore script.

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local DataStore = DataStoreService:GetDataStore("Test") -- Set to official when publishing game

local function waitForRequestBudget(requestType)
	local currentBudget = DataStoreService:GetRequestBudgetForRequestType()
	while currentBudget < 1 do
		currentBudget = DataStoreService:GetRequestBudgetForRequestType()	
		task.wait(1)
	end
end

function setupPlayerData(Player)
    local Key = "player_"..Player.UserId
    
    local leaderstats = Instance.new("Folder", Player)
    leaderstats.Name = "leaderstats"
    
    local Food = Instance.new("IntValue", leaderstats)
    Food.Value = 0
    
    local Coins = Instance.new("IntValue", leaderstats)
    Coins.Value = 0
    
    local success, returnValue
    repeat
        waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
        success, returnValue = pcall(DataStore.GetAsync, DataStore, Key)
    until success or not players:FindFirstChild(player.Name)
    
    returnValue = returnValue or {
        Food = 0, 
        Coins = 0
    }
    
    Food.Value = returnValue.Food or 0
    Coins.Value = returnValue.Coins or 0
end

function save(Player)
    local Key = "player_"..Player.UserId
    local food, coins = player.leaderstats.Food, player.leaderstats.Coins
    
    local dataTable = {
        Food = food.Value,
        Coins = coins.Value
    }
    
    local success, returnValue
    repeat
        waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
        success, returnValue = pcall(DataStore.UpdateAsync, DataStore, Key, function())
            return dataTable
        end)
    until success
end
	
for _, Player in ipairs(players:GetPlayers()) do
	task.spawn(setupPlayerData, Player)
end
players.PlayerAdded:Connect(setupPlayerData)

players.PlayerRemoving:Connect(save)
game:BindToClose(function()
    for _, Player in ipairs(players:GetPlayers()) do
        task.spawn(save, Player)
    end
    task.wait(3)
end)
    
while task.wait(60) do
	for _, Player in ipairs(players:GetPlayers()) do
		task.spawn(save, Player)
	end
end
2 Likes

Hello! :wave:
I have tried your code, and after a few fixes and tweaks, I’ve got the leaderstats to show up!
However, i’m getting an error on this line:

Error: Argument1 nil (or something like that)
following on, the data is not saving nor showing any signs of it saving
and the gui is not updating with the food/coins value :sad:

pleases get back to me as soon as you can! :slightly_smiling_face:

1 Like

@Scxiptifyz I was using your script, I didn’t notice you miss this part. Replace your old waitForRequestBudget function to the 1 below.

local function waitForRequestBudget(requestType)
	local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
	while currentBudget < 1 do
		currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)	
		task.wait(1)
	end
end
1 Like

HI Again! :wave: :slightly_smiling_face:
The error has been removed but it still does not save, nor show any sign of it saving, and the gui’s still don’t update.

If you copied the datastore script I wrote down for you I dont think there will be an issue so maybe it’s your localscript. I will have a look.

1 Like

i did change some stuff in the datastore script as when i first added the script the were some errors

@Scxiptifyz For your localscript.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local CurrencyGui = script.Parent
local CurrencyHolderFrame = CurrencyGui:WaitForChild("CurrencyHolder")
local Food = CurrencyHolderFrame:WaitForChild("Food")
local Coins = CurrencyHolderFrame:WaitForChild("Coins")

local leaderstats = player:WaitForChild("leaderstats")

local function changeValue(name: string, StatsDisplay: number)
	if name == "Food" then
		Food.Frame.StatsDisplay.Text = StatsDisplay.."/30"
	elseif name == "Coins" then
		Coins.Frame.StatsDisplay.Text = StatsDisplay
	end
	
end

changeValue("Food", leaderstats.Food.Value)
changeValue("Coins", leaderstats.Coins.Value)

leaderstats.Food.Changed:Connect(function()
	changeValue("Food", leaderstats.Food.Value)
end)
leaderstats.Coins.Changed:Connect(function()
    changeValue("Coins", leaderstats.Coins.Value)
end)

NOTE: Make sure when you are updating the values on your leaderstats you are updating on the server and not the client or it will not save.

2 Likes

The ui is now updating, thanks! but its still not updating. the changes i made is i had to delete a bracket and had to add a capital p on player

So… Are you still experiencing any issues :sweat_smile:?

1 Like

not in the output but its just not saving