Why are my tools not appearing in the inventory

why are my tools not appearing in my inventory? it literally prints “creating tool”
dw about the datastore and module

please help ive been trying to fix this for the last 5 days

	for i,v in ipairs(data['ItemTable']) do
			local foods = game.ReplicatedStorage.food
			local tools = foods.tools
			print(v .. ' is in itemtable.')
			
			for i, tool in pairs(tools:GetChildren()) do
				if tool.Name == v then
					print('creating tool')
					local newtool = tool:Clone()
					newtool.Name = v
					newtool.Handle.Anchored = false
					newtool.Parent = plr.Backpack
					module.ItemUse(newtool.Handle:FindFirstChild("LuckBooster").Value, newtool, newtool.Handle:FindFirstChild('Time').Value, plr, v)
					
				else
					print('nope')
				end
			end
		end
1 Like

Can you make the full code visible please. So we can see where the plr variable comes from

2 Likes

Hey kingbob, here is the full code:

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local DataStoreService = game:GetService("DataStoreService")
local rollstore = DataStoreService:GetDataStore("rollstore")
local module = require(script.Parent.Parent.Spawn.SeparateScript)

local function load(plr:Player)
	local folder = Instance.new("Folder", plr)
	folder.Name = 'leaderstats'
	local rolls = Instance.new("IntValue", folder)
	rolls.Name = 'Rolls'
	rolls.Value = 0

	local luckmultiplier = Instance.new("IntValue", folder)
	luckmultiplier.Name = 'Luck'
	luckmultiplier.Value = 1
	luckmultiplier.Changed:Connect(function(newvalue)
		luckmultiplier.Value = newvalue
		print(newvalue)
	end)

	local playerid = "Player_" .. plr.UserId
	local data
	local success, err = pcall(function()
		data = rollstore:GetAsync(playerid)
	end)
	if data then
		if data['Rolls'] then
			rolls.Value = data['Rolls']
		else
			rolls.Value = 0
		end

	else
		rolls.Value = 0
	end
	if data['ItemTable'] then
		print('ItemTable is not nil')
		for i,v in ipairs(data['ItemTable']) do
			local foods = game.ReplicatedStorage.food
			local tools = foods.tools
			print(v .. ' is in itemtable.')
			
			for i, tool in pairs(tools:GetChildren()) do
				if tool.Name == v then
					print('creating tool')
					local newtool = tool:Clone()
					newtool.Name = v
					newtool.Handle.Anchored = false
					newtool.Parent = plr.Backpack
					module.ItemUse(newtool.Handle:FindFirstChild("LuckBooster").Value, newtool, newtool.Handle:FindFirstChild('Time').Value, plr, v)
					
				else
					print('nope')
				end
			end
		end

	else
		print('its nil rn')
	end
end

local function save(plr:Player)
	local items = {}
	local backpack = plr.Backpack


	for i, item in backpack:GetChildren() do
		table.insert(items, item.Name)
		print(item.Name)
	end

	for i, itemers in ipairs(items) do
		print(itemers)
	end
	local datatobesaved = {
		Rolls = plr.leaderstats.Rolls.Value,
		ItemTable = items

	}

	local playerid = "Player_" .. plr.UserId

	local success, err = pcall(function()
		rollstore:UpdateAsync(playerid, function(old)
			for i, v in pairs(datatobesaved) do
				if i == nil or v == nil then
					datatobesaved = old
					print('datasave = old')
				end
			end

			--you can add individual checks to make sure each data element
			--is there and ready to be saved.

			return datatobesaved
		end)
	end)

	if success then
		print("data saved!")
	else
		print("data failed to save!")
	end
end

local function onGameClose()
	if runService:IsStudio() then task.wait(3) return nil end
	for _, player in ipairs(players:GetPlayers()) do
		save(player)
	end
	task.wait(3)
end

players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(onGameClose)
2 Likes

If the value of this changes why do you then manually change it? The reason this event has been fired is because the value has changed so their is no need to set it to it’s own value

2 Likes

k i will remove that line of code

1 Like

Do any errors occur? Have you tried printing the parent of the tool after a seconds delay?

1 Like

yes, it says the tool is in the backpack (prints backpack)

2 Likes

Have you looked inside of the backpack when playtesting

2 Likes

Yes. Theres a 50/50 my code works on roblox studio
But it never works in roblox

2 Likes

I have an idea, make the new tool variable at the start of the script like this:

local newtool = nil

then afterwards, you set the variable with the tools clones.
You could just be resetting it.
If not, talk to me I’ll try find another issue

2 Likes

at the of the script or the for i,v loop?

3 Likes

Top of the script, not in the loop

2 Likes

this still works on roblox studio, but not roblox… i am confused

3 Likes

I’ve had that problem before, try it on the server, if it works on the server then it seems like it ONLY works on the server and not client, I’ll try help you then

1 Like

wdym by this? i am not sure to understand…

1 Like

It might ONLY work on the server, try check that first, then come back to me

1 Like

Yo,
Roblox Studio
Items print locally: yes
items print server: yes

Roblox
items print server: yes
items print local: no

But i think it might be related to this error?

i am guessing it is bcz the plr left so “plr.Character” is nil
if i am right how could i make plr.character still usable

local function save(plr)
	local items = {}
	
	local essence = plr.Character:FindFirstChild("Head").essence -- line 82
	local function RGBstringToColor3(RGBstring)
return tostring(RGBstring:ToHex())
	end
	local essencecolor = RGBstringToColor3(essence.BillBoardGui.Essence.TextColor3)

local essencesave =  {
		EssenceName = essence.BillBoardGui.Essence.Text,
		EssenceColor = essencecolor,
		EssenceFont = essence.BillBoardGui.Essence.Font.Name, 
		Chance = essence.chance.BillBoardGui.Chance.Text,
	}
	local backpack = plr.Backpack

	for i, item in backpack:GetChildren() do
		table.insert(items, item.Name)
		print(item.Name)
	end

	for i, itemers in ipairs(items) do
		print(itemers)
	end
	local datatobesaved = {
		Rolls = plr.leaderstats.Rolls.Value,
		ItemTable = items,
		CurrentEssence = essencesave,
		
	}

	local playerid = "Player_" .. plr.UserId

	local success, err = pcall(function()
		rollstore:UpdateAsync(playerid, function(old)
			for i, v in pairs(datatobesaved) do
				if i == nil or v == nil then
					datatobesaved = old
					print('datasave = old')
				end
			end

			--you can add individual checks to make sure each data element
			--is there and ready to be saved.

			return datatobesaved
		end)
	end)

	if success then
		print("data saved! (server)")
	else
		print("data failed to save!")
	end
end
1 Like

Try define the character like this to make the character exist:

local char = plr.Character or plr.CharacterAdded:Wait()

1 Like

hey dude, i am still confused by out of the blue my code starting working with no real modifications?
so my problem solved, but immense thank you for your help!

2 Likes