local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local player = game.Players.PlayerAdded
local chanceList = require(replicatedStorage.Modules:WaitForChild("Chancelist"))
local dataStore = dataStoreService:GetDataStore("Test1")
local function waitForRequestBudget(requestType)
local currentBudget = dataStore:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
end
end
local function setupLeaderstats(player)
local userID = player.UserId
local key = "Player_"..userID
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = 'leaderstats'
local rolls = Instance.new("NumberValue", leaderstats)
rolls.Name = "Rolls"
rolls.Value = 0
local inventory = Instance.new("Folder", player)
inventory.Name = "Inventory"
local equippedChance = Instance.new("StringValue", inventory)
equippedChance.Name = "EquippedChance"
equippedChance.Value = "None"
local inventoryFolder = Instance.new("Folder", inventory)
inventoryFolder.Name = "OwnedChances"
for i, v in ipairs(chanceList) do
local newInstance = Instance.new("BoolValue", inventoryFolder)
newInstance.Name = v[1]
end
local success, returnValue
repeat
waitForRequestBudget(Enum)
success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
until success or not Players:FindFirstChild(player.Name)
if success then
if returnValue == nil then
returnValue = {
Rolls = 0,
}
end
player.leaderstats.Rolls.Value = if returnValue.Rolls ~= nil then returnValue.Rolls else 0
player.Inventory.EquippedChance.Value = if returnValue.EquippedChance ~= nil then returnValue.EquippedChance else "None"
for i,v in ipairs(player.Inventory.OwnedChances:GetChildren()) do
v.Value = if returnValue.Inventory.OwnedChances[v.Name] ~= nil then returnValue.Inventory.OwnedChances[v.Name] else false
end
else
player:Kick("Data loading Error, Please Try again")
end
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local rolls = player.leaderstats.Rolls.Value
local equippedChance = player.Inventory.EquippedChance.Value
local inventoryTable = {}
for i, chance in ipairs(player.Inventory.OwnedChances:GetChildren()) do
inventoryTable[chance.Name] = chance.Value
end
local dataTable = {
Rolls = rolls,
Inventory = {
EquippedChance = equippedChance,
OwnedChances = inventoryTable
}
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
return dataTable
end)
until success
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(function(p)
setupLeaderstats(p)
end)(player)
end
game.Players.PlayerAdded:Connect(setupLeaderstats)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
while true do
task.wait(600)
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end
What do you mean by local player = game.Players.PlayerAdded?
Setting a variable to Players.PlayerAdded just returns the signal and not the player as my knowledge
Also if this is the complete script, you don’t use it anywhere
local currentBudget = dataStore:GetRequestBudgetForRequestType(requestType) :GetRequestBudgetForRequestType() is a DataStoreService method, not a DataStores
repeat
waitForRequestBudget(Enum) -- You are sending only Enum here, which breaks the function.
success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
until success or not Players:FindFirstChild(player.Name)
Aside from these the script works fine for me without errors:
local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local chanceList = require(replicatedStorage.Modules:WaitForChild("Chancelist"))
local dataStore = dataStoreService:GetDataStore("Test1")
local function waitForRequestBudget(requestType)
local currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
task.wait()
currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
end
end
local function setupLeaderstats(player)
local userID = player.UserId
local key = "Player_"..userID
local leaderstats = Instance.new("Folder")
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local rolls = Instance.new("NumberValue")
rolls.Name = "Rolls"
rolls.Value = 0
rolls.Parent = leaderstats
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = player
local equippedChance = Instance.new("StringValue")
equippedChance.Name = "EquippedChance"
equippedChance.Value = "None"
equippedChance.Parent = inventory
local inventoryFolder = Instance.new("Folder")
inventoryFolder.Name = "OwnedChances"
inventoryFolder.Parent = inventory
for i, v in ipairs(chanceList) do
local newInstance = Instance.new("BoolValue")
newInstance.Name = v[1]
newInstance.Parent = inventoryFolder
end
local success, returnValue
repeat
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
until success or not Players:FindFirstChild(player.Name)
if not Players:FindFirstChild(player.Name) then return end
if success then
if returnValue == nil then
returnValue = {
Rolls = 0,
}
end
player.leaderstats.Rolls.Value = if returnValue.Rolls ~= nil then returnValue.Rolls else 0
player.Inventory.EquippedChance.Value = if returnValue.EquippedChance ~= nil then returnValue.EquippedChance else "None"
for i,v in ipairs(player.Inventory.OwnedChances:GetChildren()) do
v.Value = if returnValue.Inventory and returnValue.Inventory.OwnedChances and returnValue.Inventory.OwnedChances[v.Name] ~= nil then returnValue.Inventory.OwnedChances[v.Name] else false
end
else
player:Kick("Data loading Error, Please Try again")
end
until true
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local rolls = player.leaderstats.Rolls.Value
local equippedChance = player.Inventory.EquippedChance.Value
local inventoryTable = {}
for i, chance in ipairs(player.Inventory.OwnedChances:GetChildren()) do
inventoryTable[chance.Name] = chance.Value
end
local dataTable = {
Rolls = rolls,
Inventory = {
EquippedChance = equippedChance,
OwnedChances = inventoryTable
}
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
return dataTable
end)
until success
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(function(p)
setupLeaderstats(p)
end)(player)
end
game.Players.PlayerAdded:Connect(setupLeaderstats)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
while true do
task.wait(600)
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end
local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
--local chanceList = require(replicatedStorage.Modules:WaitForChild("Chancelist"))
local dataStore = dataStoreService:GetDataStore("Test1")
local function waitForRequestBudget(requestType)
local currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
end
end
local function setupLeaderstats(player)
local userID = player.UserId
local key = "Player_"..userID
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = 'leaderstats'
local rolls = Instance.new("NumberValue", leaderstats)
rolls.Name = "Rolls"
rolls.Value = 0
local inventory = Instance.new("Folder", player)
inventory.Name = "Inventory"
local equippedChance = Instance.new("StringValue", inventory)
equippedChance.Name = "EquippedChance"
equippedChance.Value = "None"
local inventoryFolder = Instance.new("Folder", inventory)
inventoryFolder.Name = "OwnedChances"
--for i, v in ipairs(chanceList) do
-- local newInstance = Instance.new("BoolValue", inventoryFolder)
-- newInstance.Name = v[1]
--end
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
until success or not Players:FindFirstChild(player.Name)
if success then
if returnValue == nil then
returnValue = {
Rolls = 0,
}
end
player.leaderstats.Rolls.Value = if returnValue.Rolls ~= nil then returnValue.Rolls else 0
player.Inventory.EquippedChance.Value = if returnValue.EquippedChance ~= nil then returnValue.EquippedChance else "None"
for i,v in ipairs(player.Inventory.OwnedChances:GetChildren()) do
v.Value = if returnValue.Inventory.OwnedChances[v.Name] ~= nil then returnValue.Inventory.OwnedChances[v.Name] else false
end
else
player:Kick("Data loading Error, Please Try again")
end
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local rolls = player.leaderstats.Rolls.Value
local equippedChance = player.Inventory.EquippedChance.Value
local inventoryTable = {}
for i, chance in ipairs(player.Inventory.OwnedChances:GetChildren()) do
inventoryTable[chance.Name] = chance.Value
end
local dataTable = {
Rolls = rolls,
Inventory = {
EquippedChance = equippedChance,
OwnedChances = inventoryTable
}
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
return dataTable
end)
until success
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(function(p)
setupLeaderstats(p)
end)(player)
end
game.Players.PlayerAdded:Connect(setupLeaderstats)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
while true do
task.wait(600)
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end
This works fine for me, after fixing the things that I’ve stated before. Only thing I changed is I commented out the chanceList module and the function of it:
local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local chanceList = require(replicatedStorage.Modules:WaitForChild("Chancelist"))
local dataStore = dataStoreService:GetDataStore("Test1")
local function waitForRequestBudget(requestType)
local currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
end
end
local function setupLeaderstats(player)
local userID = player.UserId
local key = "Player_"..userID
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = 'leaderstats'
local rolls = Instance.new("NumberValue", leaderstats)
rolls.Name = "Rolls"
rolls.Value = 0
local inventory = Instance.new("Folder", player)
inventory.Name = "Inventory"
local equippedChance = Instance.new("StringValue", inventory)
equippedChance.Name = "EquippedChance"
equippedChance.Value = "None"
local inventoryFolder = Instance.new("Folder", inventory)
inventoryFolder.Name = "OwnedChances"
for i, v in ipairs(chanceList) do
local newInstance = Instance.new("BoolValue", inventoryFolder)
newInstance.Name = v[1]
end
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
until success or not Players:FindFirstChild(player.Name)
if success then
if returnValue == nil then
returnValue = {
Rolls = 0,
}
end
player.leaderstats.Rolls.Value = if returnValue.Rolls ~= nil then returnValue.Rolls else 0
player.Inventory.EquippedChance.Value = if returnValue.EquippedChance ~= nil then returnValue.EquippedChance else "None"
for i,v in ipairs(player.Inventory.OwnedChances:GetChildren()) do
v.Value = if returnValue.Inventory.OwnedChances[v.Name] ~= nil then returnValue.Inventory.OwnedChances[v.Name] else false
end
else
player:Kick("Data loading Error, Please Try again")
end
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local rolls = player.leaderstats.Rolls.Value
local equippedChance = player.Inventory.EquippedChance.Value
local inventoryTable = {}
for i, chance in ipairs(player.Inventory.OwnedChances:GetChildren()) do
inventoryTable[chance.Name] = chance.Value
end
local dataTable = {
Rolls = rolls,
Inventory = {
EquippedChance = equippedChance,
OwnedChances = inventoryTable
}
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
return dataTable
end)
until success
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(function(p)
setupLeaderstats(p)
end)(player)
end
game.Players.PlayerAdded:Connect(setupLeaderstats)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
while true do
task.wait(600)
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end