Saving Inventory Not Working

Hey! Im making an RNG game and followed an in depth tutorial. Although this tutorial was so “in-depth” they didn’t include inventory saving. So, as a not-so-great scripter, i would like to get some help on this.

Although I did get some help, we didn’t get as far as troubleshooting, which is totally okay.

Video of this issue:

Rejoining may keep the last aura equipped over the players head, but will not save in the inventory ui

Im so lost. and i just need this to save before i explode

you didn’t provide code, so no one can solve it :pensive:

1 Like

what code should i provide? the inventory, the rolling?

yk what ill include both:

adding inventory:

local RS = game:GetService("ReplicatedStorage")
local updateInventoryEvent = RS:WaitForChild("UpdateInventory")

local ScrollingFrame = script.Parent.ScrollingFrame
local EffectTitles = ScrollingFrame:WaitForChild("EffectTitles")

local function addItem(effect)
	
	local clonedTitle = EffectTitles:WaitForChild(effect):Clone()
	clonedTitle.Parent = ScrollingFrame
	clonedTitle.Visible = true
	
end

updateInventoryEvent.OnClientEvent:Connect(addItem)

roll:

local DSS = game:GetService("DataStoreService")
local mainDS = DSS:GetDataStore("Rolls")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local rollEvent = ReplicatedStorage:WaitForChild("Roll")
local showRollsResultEvent = ReplicatedStorage:WaitForChild("ShowRollsResult")
local updateInventoryEvent = ReplicatedStorage:WaitForChild("UpdateInventory")

local chances = {
	["Common"] = 500, -- 50% chance
	["Uncommon"] = 300, -- 30% chance
	["Rare"] = 250, -- 25% chance
	["Aquatic"] = 150, -- 15% chance
	["Epic"] = 100, -- 10% chance
	["Electric"] = 85, -- 8.5% chance
	["Legendary"] = 75, -- 7.5% chance
	["Guilt"] = 50, -- 5% chance
	["Stinky"] = 25, -- 2.5% chance
	["Ignited"] = 10, -- 1% chance
	["Godlike"] = 5, -- 0.5% chance
}

local function roll(player)
	-- Increment the Rolls value in leaderstats
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local Rolls = leaderstats:FindFirstChild("Rolls")
		if Rolls then
			Rolls.Value = Rolls.Value + 1
		end
	end

	-- Roll logic and updating player's inventory
	local PlayerDataFolder = game.ServerStorage:WaitForChild("PlayerData"):WaitForChild(player.UserId)
	local InventoryFolder = PlayerDataFolder:WaitForChild("Inventory")

	local listOfResults = {}

	for count = 1, 11 do
		local random = math.random(1, 1000)
		local counter = 0

		for effect, weight in pairs(chances) do
			counter = counter + weight
			if random <= counter then
				table.insert(listOfResults, effect)
				break
			end
		end
	end

	showRollsResultEvent:FireClient(player, listOfResults)

	task.wait(3.25)

	local newEffect = listOfResults[11]
	local existingItem = InventoryFolder:FindFirstChild(newEffect)

	if existingItem then
		-- Increment the existing item's value
		if existingItem:IsA("IntValue") then
			existingItem.Value = existingItem.Value + 1
		else
			warn("Expected IntValue but found " .. existingItem.ClassName)
		end
	else
		-- Create a new item and set its initial value to 1
		local newItem = Instance.new("IntValue")
		newItem.Name = newEffect
		newItem.Value = 1
		newItem.Parent = InventoryFolder
	end

	updateInventoryEvent:FireClient(player, newEffect)
end

rollEvent.OnServerEvent:Connect(roll)

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local Rolls = Instance.new("IntValue", leaderstats)
	Rolls.Name = "Rolls"

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

	if success then
		if data then
			Rolls.Value = data[1]
		else
			Rolls.Value = 0 -- Default value if no data is found
		end
	else
		warn("Failed to load data for player " .. player.Name)
		Rolls.Value = 0 -- Default value if loading fails
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local Rolls = leaderstats:FindFirstChild("Rolls")
		if Rolls then
			local success, err = pcall(function()
				mainDS:SetAsync(player.UserId, {Rolls.Value})
			end)

			if not success then
				warn("Failed to save data for player " .. player.Name .. ": " .. err)
			end
		else
			warn("Rolls not found for player " .. player.Name)
		end
	else
		warn("Leaderstats not found for player " .. player.Name)
	end
end)

the roll is like 80% probably the problem because i was trying to make it save when you leave (WHICH I FAILED BTW AND I NEED HELP SO IF YOU COULD ASSIST ME Save Inventory After Leaving - #9 by JamesBlossoms PLEASE THANK U)