Datastore headache

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("Data")

local https = game:GetService("HttpService")
local url = i accidentally left this in and some fucker spammed the n word
local function load(plr)
	ds:GetAsync({plr.UserId})
end
--code will literally run on tooth picks and gum
local leadstats
local nights
local maxmode
local function save(plr, datavalue)
	ds:SetAsync(plr.UserId, datavalue)
end
local function senderror(plr)
end
game.Players.PlayerAdded:Connect(function(plr)
	--value mania
	leadstats = Instance.new("Folder", plr)
	leadstats.Name = "leadstats"
	nights = Instance.new("IntValue", leadstats)
	nights.Name = "nights"
	maxmode = Instance.new("BoolValue", leadstats)
	maxmode.Name = "maxmode"
	function senderror(plr)
		local message = "Something may have went terribly wrong lol ".. plr.UserId.."/"..plr.Name.. " ".. "nights: "..nights.Value.. " maxmode: ".. tostring(maxmode.Value)
		local info = {
			["embeds"] = {{
				["title"] = "Datastore failure",
				["description"] = message
			}}
		}
		local encrpyted = https:JSONEncode(info)
		https:PostAsync(url, encrpyted)
	end
	--that took way too long
	local s, r
	s, r = pcall(function()
		load(plr)
		warn("loaded")
		if not s then
			warn("failed loading")
			senderror()
			repeat load(plr) warn("Failed retry") until s
		end
	end)
end)
local datatalbe = {}
game.Players.PlayerRemoving:Connect(function(plr)
	local s, r
	game:BindToClose(function()
	s, r = pcall(function()
		for _, vals in pairs(leadstats:GetChildren()) do
			table.insert(datatalbe, vals.Value)
			warn("Added to table: ".. vals.Name)
			end
		save(plr, datatalbe)
	end)
	if not s then
		warn	("Saving failed")
		repeat 
			s, r = pcall(function()
					for _, vals in pairs(leadstats:GetChildren()) do
					table.insert(datatalbe, vals.Value)
					warn("Added to table: ".. vals.Name)
				end
			end)
			warn("retrying saving") until s
		senderror()
		end
		save(plr, datatalbe)
	end)
end)

aaaa

output:
image

You’re sending too much requests at one time
You can try using this code example to avoid this problem:

local function load(plr)
	local keysToLoad = {plr.UserId}
	local dataValues = ds:GetAsync(keysToLoad)
	-- do smth with the datavalues
end

2 Likes

i am late but this is ltierally my code except i didn’t set a variable for the userid.

You cannot provide an array of keys to GlobalDataStore:GetAsync().

Try this:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Data")
local HttpService = game:GetService("HttpService")

local URL = "" -- Roblox moderates your code, I suggest you just don't write any stuff that break the tos

local function LoadData(player)
    local success, data = pcall(function()
       return DataStore:GetAsync(player.UserId)
    end)

    if success and data then
        return data
    else
        warn("Failed to load data for player " .. player.Name .. ", retrying...")
        return LoadData(player)
    end
end

local function SaveData(player, data)
    local success = pcall(function()
        DataStore:SetAsync(player.UserId, data)
    end)

    if not success then
        warn("Failed to save data for player " .. player.Name .. ", retrying...")
        SaveData(player, data)
    end
end

local function SendError(player, nights, maxmode)
    local message = "Something went wrong with DataStore for player " .. player.Name .. " (ID: " .. player.UserId .. "), nights: " .. nights .. ", maxmode: " .. tostring(maxmode)
    local info = {
        ["embeds"] = {{
            ["title"] = "DataStore Failure",
            ["description"] = message
        }}
    }

    local encodedData = HttpService:JSONEncode(info)
    HttpService:PostAsync(URL, encodedData)
end

local function BindEvents(player)
    local leadstats = Instance.new("Folder", player)
    leadstats.Name = "leadstats"

    local nights = Instance.new("IntValue", leadstats)
    nights.Name = "nights"
    nights.Value = 0

    local maxmode = Instance.new("BoolValue", leadstats)
    maxmode.Name = "maxmode"
    maxmode.Value = false

    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            SaveData(player, leadstats:GetChildren())
        end)
    end)
end

game.Players.PlayerAdded:Connect(function(player)
    local data = LoadData(player) or {}

    bindEvents(player)

    for _, value in pairs(data) do
        local foundValue = player.leadstats:FindFirstChild(value.Name)
        if foundValue then
            foundValue.Value = value.Value
        else
            value:Clone().Parent = player.leadstats
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    SaveData(player, player.leadstats:GetChildren())
end)

This should work with some error handling your code should work properly

1 Like

You cannot save instances to Data Stores. I recommend saving the values to a table then saving that table.

oh yeah lol, this should be fixed now:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Data")
local HttpService = game:GetService("HttpService")

local URL = "" -- Roblox moderates your code, I suggest you just don't write any stuff that break the tos

local function LoadData(player)
    local success, data = pcall(function()
       return DataStore:GetAsync(player.UserId)
    end)

    if success and data then
        return data
    else
        warn("Failed to load data for player " .. player.Name .. ", retrying...")
        return LoadData(player)
    end
end

local function SaveData(player, data)
    local success = pcall(function()
        DataStore:SetAsync(player.UserId, data)
    end)

    if not success then
        warn("Failed to save data for player " .. player.Name .. ", retrying...")
        SaveData(player, data)
    end
end

local function SendError(player, nights, maxmode)
    local message = "Something went wrong with DataStore for player " .. player.Name .. " (ID: " .. player.UserId .. "), nights: " .. nights .. ", maxmode: " .. tostring(maxmode)
    local info = {
        ["embeds"] = {{
            ["title"] = "DataStore Failure",
            ["description"] = message
        }}
    }

    local encodedData = HttpService:JSONEncode(info)
    HttpService:PostAsync(URL, encodedData)
end

local function BindEvents(player)
    local leadstats = Instance.new("Folder", player)
    leadstats.Name = "leadstats"

    local data = LoadData(player) or {}
    local nightsValue = Instance.new("IntValue", leadstats)
    nightsValue.Name = "nights"
    nightsValue.Value = data.nights or 0

    local maxmodeValue = Instance.new("BoolValue", leadstats)
    maxmodeValue.Name = "maxmode"
    maxmodeValue.Value = data.maxmode or false

    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            SaveData(player, {
                nights = nightsValue.Value,
                maxmode = maxmodeValue.Value
            })
        end)
    end)
end

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

game.Players.PlayerRemoving:Connect(function(player)
    local leadstats = player:FindFirstChild("leadstats")
    if leadstats then
        SaveData(player, {
            nights = leadstats.nights.Value,
            maxmode = leadstats.maxmode.Value
        })
    end
end)

1 Like

These two pcall functions can be simplified:

-- Original
    local success, data = pcall(function()
       return DataStore:GetAsync(player.UserId)
    end)

    local success = pcall(function()
        DataStore:SetAsync(player.UserId, data)
    end)

-- Simplified
local success, response = pcall(DataStore.GetAsync, DataStore, player.UserId)

local success = pcall(DataStore.SetAsync, DataStore, player.UserId, data)

Pcalls take a function to run and catches the error. Since there is only one function being executed within the pcalls, you should just pass the function being called. You can look at this post for more information:

Your code appears to have a few problems, but I rewrote it for you. Test this out x

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("Data")

local https = game:GetService("HttpService")
local url = "https://example.com" -- replace with your URL

local function load(plr)
    ds:GetAsync(plr.UserId)
end

local leadstats
local nights
local maxmode

local function save(plr, datavalue)
    ds:SetAsync(plr.UserId, datavalue)
end

local function senderror(plr)
    local message = "Something may have went terribly wrong lol ".. plr.UserId.."/"..plr.Name.. " ".. "nights: "..nights.Value.. " maxmode: ".. maxmode.Value
    local info = {
        ["embeds"] = {{
            ["title"] = "Datastore failure",
            ["description"] = message
        }}
    }
    local encrypted = https:JSONEncode(info)
    https:PostAsync(url, encrypted)
end

game.Players.PlayerAdded:Connect(function(plr)
    --value mania
    leadstats = Instance.new("Folder", plr)
    leadstats.Name = "leadstats"
    nights = Instance.new("IntValue", leadstats)
    nights.Name = "nights"
    maxmode = Instance.new("BoolValue", leadstats)
    maxmode.Name = "maxmode"
    function senderror(plr)
        local message = "Something may have went terribly wrong lol ".. plr.UserId.."/"..plr.Name.. " ".. "nights: "..nights.Value.. " maxmode: ".. maxmode.Value
        local info = {
            ["embeds"] = {{
                ["title"] = "Datastore failure",
                ["description"] = message
            }}
        }
        local encrypted = https:JSONEncode(info)
        https:PostAsync(url, encrypted)

These variables should be within the PlayerAdded function, not at the top level.

local leadstats
local nights
local maxmode

Instead of Pcalls I just used the success values Like this:
I also changed some of the code but it should function better now

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Data")
local HttpService = game:GetService("HttpService")

local URL = "" -- Roblox moderates your code, I suggest you just don't write any stuff that break the tos

local function LoadData(player)
    local success, data = DataStore:GetAsync(player.UserId)
    if not success then
        warn("Failed to load data for player " .. player.Name .. ", retrying...")
        LoadData(player)
    end
    return data
end

local function SaveData(player, data)
    local success = DataStore:SetAsync(player.UserId, data)
    if not success then
        warn("Failed to save data for player " .. player.Name .. ", retrying...")
        SaveData(player, data)
    end
end

local function SendError(player, nights, maxmode)
    local message = "Something went wrong with DataStore for player " .. player.Name .. " (ID: " .. player.UserId .. "), nights: " .. nights .. ", maxmode: " .. tostring(maxmode)
    local info = {
        ["embeds"] = {{
            ["title"] = "DataStore Failure",
            ["description"] = message
        }}
    }

    HttpService:PostAsync(URL, HttpService:JSONEncode(info))
end

local function BindEvents(player)
    local leadstats = Instance.new("Folder", player)
    leadstats.Name = "leadstats"

    local data = LoadData(player) or {}
    local nightsValue = Instance.new("IntValue", leadstats)
    nightsValue.Name = "nights"
    nightsValue.Value = data.nights or 0

    local maxmodeValue = Instance.new("BoolValue", leadstats)
    maxmodeValue.Name = "maxmode"
    maxmodeValue.Value = data.maxmode or false

    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            SaveData(player, {
                nights = nightsValue.Value,
                maxmode = maxmodeValue.Value
            })
        end)
    end)
end

game.Players.PlayerAdded:Connect(BindEvents)

game.Players.PlayerRemoving:Connect(function(player)
    local leadstats = player:FindFirstChild("leadstats")
    if leadstats then
        SaveData(player, {
            nights = leadstats.nights.Value,
            maxmode = leadstats.maxmode.Value
        })
    end
end)

The success value originates from the pcalls. Take a look at the Roblox documentation for more information:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.