Issue with Saving Tools in Roblox Studio

  1. What do you want to achieve? Keep it simple and clear!

I want to successfully save and load player tools. Specifically, I need my code to ensure that when a player acquires a tool the tool saves to the database.

  1. What is the issue? Include screenshots / videos if possible!

I am encountering an issue with saving player tools in Roblox Studio. During gameplay, a Boolean value associated to a tool is set to true. Consequently, when the player re-enters the game, the tool that should appear in their inventory based on this Boolean value does not load. though, I dont suspect there might be an error in my code related to the saving and loading process because it already works with currency. I suspect it being something with the syntax.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried searching for general solutions related to saving and loading, but my problem seems too specific to find relevant answers.

Here is the loaddata function where I believe the problem lies:

function loaddata(plr)
	local userid = plr.UserId
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local boolvalues = Instance.new("Folder")
	boolvalues.Name = "boolvalues"
	boolvalues.Parent = plr

	local doubloons = Instance.new("IntValue")
	doubloons.Name = "Doubloons"
	doubloons.Parent = leaderstats
	
	local tooltable = {}
	
	for _, v in pairs(toolfolder:GetChildren()) do
		local value = Instance.new("BoolValue")
		value.Name = "owns" .. v.Name

		value.Parent = boolvalues
		tooltable[value.Name] = false
	end
	


	local success
	local playerdata
	
	local attempt = 1

	repeat
		success, playerdata = pcall(function()
			return db:GetAsync(userid, playerdata)
		end)

		attempt += 1
		if not success then
			task.wait(2)
		end
		
	until success or attempt == 5

	if success then
		print("loaded players data")
		if not playerdata then
			print("assigning default data")
			playerdata = {
				["Doubloons"] = 0,
				Inventory = tooltable
			}
		end
		currentdata[userid] = playerdata
	else
		plr:Kick("Unable to assign data")
	end

	doubloons.Value = currentdata[userid].Doubloons
	
	
	-- I suspect it being wrong somewhere in here
	for _, v in pairs(boolvalues:GetChildren()) do
		print(v.Value)
		print(currentdata[userid].Inventory[v.Name])
		
		v.Value = currentdata[userid].Inventory[v.Name]
		
		v.Changed:Connect(function()
			currentdata[userid][v.Name] = v.Value
			print(currentdata[userid][v.Name])
		end)
		
	end
	
	doubloons.Changed:Connect(function()
		currentdata[userid].Doubloons = doubloons.Value
	end)
	

	leaderstats.Parent = plr
	loadtools(plr)
	

end
1 Like

I think its a problem with the loadtools function.
Could you show the loadtools code? I tested your script, for me it’s working.
image

1 Like

Here is the loadtools function, which encompasses the createtool function.
(I didn’t create these myself, so I’m not fully familiar with these functions)


function createtool(player, name)
	local user = player.UserId
	
	name = string.sub(name, 5)

	if not player.Backpack:FindFirstChild(name) and player.Character and not player.Character:FindFirstChild(name) then

		local tool = toolfolder:FindFirstChild(name):Clone()
		tool.Parent = player.Backpack

	end

end

function loadtools(plr)
	local user = plr.UserId
	
	print(currentdata[user].Inventory)
	for tool, value in pairs(currentdata[user].Inventory) do
		if value == true then
			print(tool)
			createtool(plr, tool)
		end
	end
end

I don’t believe there’s anything wrong with these functions because I connected a changed event to each Boolean value and printed the current data and Boolean value, both of which were true. Additionally, these functions do not change ‘currentdata’ at all.

It was simply just a syntax error. I was probably tired when posting this topic

here was the code before

currentdata[userid][v.Name] = v.Value

here is the working code

currentdata[userid].Inventory[v.Name] = v.Value

I forgot to put ‘.Inventory’ into the actual code but printed out the correct code.
Thanks for trying to help anyway :slight_smile:

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