Hello Roblox,
i’m currently following monzter_dev’s simulator series and i just finished the datastore, however. when i finished, my leaderstats disappeared and left this error in my local script located inside the currency gui:
Players.ScxiptedShark.PlayerGui.CurrencyGui.CurrencyDisplayManager:18: attempt to index nil with 'leaderstats'
Here is my datastore code:
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
local function setupPlayerData(player)
local UserId = player.UserId
local key = "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 sucsess, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
sucsess, returnValue = pcall(DataStore.GetAsync, DataStore, key)
until sucsess or not players:FindFirstChild(player.Name)
if sucsess then
if returnValue == nil then
returnValue = {
Food = 0,
Coins = 0,
}
end
Food.Value = if returnValue.Food ~= nil then returnValue.Food else 0
Coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
else
player:kick("An error has occured during loading your data, Roblox's Datastores might be down. Please try again later or contact us in our group!")
print(player.Name.."'s Data has failed to load!")
end
local function save(player)
local UserId = player.UserId
local key = "player_"..UserId
local food = player.leaderstats.Food.Value
local coins = player.leaderstats.Coins.Value
local dataTable = {
Food = food,
Coins = coins,
}
print(dataTable)
local sucsess, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
sucsess, returnValue = pcall(DataStore.UpdateAsync, DataStore, key, function()
return dataTable
end)
until sucsess
if sucsess then
print("Data Saved")
else
print("Data saving error")
end
end
local function onShutdown()
if RunService:IsStudio() then
task.wait(2)
else
local Finished = Instance.new("BindableEvent")
local allPlayers = players:GetPlayers()
local leftPlayers = #allPlayers
for _, player in ipairs(allPlayers) do
coroutine.wrap(function()
save(player)
leftPlayers -=1
if leftPlayers == 0 then
Finished:Fire()
end
end)
end
Finished.Event:Wait()
end
end
for _, player in ipairs(players:GetPlayers()) do
coroutine.wrap(setupPlayerData)(player)
end
players.PlayerAdded:Connect(setupPlayerData)
players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
while true do
task.wait(60)
for _, player in ipairs(players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end
end
and here is the currency display script:
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 wait(1) until player.leaderstats -- error is on this line apparently
changeValue("Food", player.leaderstats.Food.Value)
changeValue("Coins", player.leaderstats.Coins.Value)
player.leaderstats.Food.Changed:Connect(function()
changeValue("Food", player.leaderstats.Food.Value)
end)
player.leaderstats.Coins.Changed:Connect(function()
changeValue("Coins", player.leaderstats.Food.Value)
end)
please reply with a fix!
-ScxiptedShark