For loop isn't working right

Hello, my for loop script isn’t working right.
here is my current script


local plr = game.Players.LocalPlayer
local CloneTemp = script.Parent.Clonetemplate
local inv = plr:WaitForChild("inv")

game.ReplicatedStorage.E.Loadinv.OnClientEvent:Connect(function()
   for _, foods in pairs(plr.inv:GetChildren())do
   	print("Message send")
   	wait()
   	local clone = CloneTemp:Clone()
   	clone.Parent = script.Parent
   	clone.Visible = true
   	clone.TextButton.Text = foods.Name
   end
end)

game.ReplicatedStorage.E.addtemplate.OnClientEvent:Connect(function(ItemName)
   local clonetemplate = CloneTemp:Clone()
   clonetemplate.TextButton.Text = ItemName
   clonetemplate.Parent = script.Parent
   clonetemplate.Visible = true

end)


this script is supose to loop through all the item in the player inventory 
it worked for the first time i tested it but the second time for some reason it didn't work
what's wrong with it?
1 Like

Could you explain how doesn’t it work?
Did it only print message sended once?

can you please use ``` to format your text as script so its easier to understand
like this

this is a code block

like this

local plr = game.Players.LocalPlayer
local CloneTemp = script.Parent.Clonetemplate
local inv = plr:WaitForChild(“inv”)

game.ReplicatedStorage.E.Loadinv.OnClientEvent:Connect(function()
print(“message sended”)
for _, foods in pairs(plr.inv:GetChildren())do
wait()
local clone = CloneTemp:Clone()
clone.Parent = script.Parent
clone.Visible = true
clone.TextButton.Text = foods.Name
end
end)

game.ReplicatedStorage.E.addtemplate.OnClientEvent:Connect(function(ItemName)
local clonetemplate = CloneTemp:Clone()
clonetemplate.TextButton.Text = ItemName
clonetemplate.Parent = script.Parent
clonetemplate.Visible = true

end)

@supercronter4

local plr = game.Players.LocalPlayer
local CloneTemp = script.Parent.Clonetemplate
local inv = plr:WaitForChild(“inv”)

game.ReplicatedStorage.E.Loadinv.OnClientEvent:Connect(function()
for _, foods in pairs(plr.inv:GetChildren())do
print(“message sended”)
wait()
local clone = CloneTemp:Clone()
clone.Parent = script.Parent
clone.Visible = true
clone.TextButton.Text = foods.Name
end
end)

game.ReplicatedStorage.E.addtemplate.OnClientEvent:Connect(function(ItemName)
local clonetemplate = CloneTemp:Clone()
clonetemplate.TextButton.Text = ItemName
clonetemplate.Parent = script.Parent
clonetemplate.Visible = true

end)

local plr = game.Players.LocalPlayer
local CloneTemp = script.Parent.Clonetemplate
local inv = plr:WaitForChild(“inv”)

game.ReplicatedStorage.E.Loadinv.OnClientEvent:Connect(function()
for _, foods in pairs(plr.inv:GetChildren())do
print(“Message send”)
wait()
local clone = CloneTemp:Clone()
clone.Parent = script.Parent
clone.Visible = true
clone.TextButton.Text = foods.Name
end
end)

game.ReplicatedStorage.E.addtemplate.OnClientEvent:Connect(function(ItemName)
local clonetemplate = CloneTemp:Clone()
clonetemplate.TextButton.Text = ItemName
clonetemplate.Parent = script.Parent
clonetemplate.Visible = true

end)

i cant format the script for some reason

you have to do ```lua
–then the code

"```"lua
--code
"```"
do that bit without these """"
1 Like
local plr = game.Players.LocalPlayer
local CloneTemp = script.Parent.Clonetemplate
local inv = plr:WaitForChild("inv")

game.ReplicatedStorage.E.Loadinv.OnClientEvent:Connect(function()
	for _, foods in pairs(plr.inv:GetChildren())do
		print("Message send")
		wait()
		local clone = CloneTemp:Clone()
		clone.Parent = script.Parent
		clone.Visible = true
		clone.TextButton.Text = foods.Name
	end
end)

game.ReplicatedStorage.E.addtemplate.OnClientEvent:Connect(function(ItemName)
	local clonetemplate = CloneTemp:Clone()
	clonetemplate.TextButton.Text = ItemName
	clonetemplate.Parent = script.Parent
	clonetemplate.Visible = true

end)

@supercronter4 what part of this isnt looping

game.ReplicatedStorage.E.Loadinv.OnClientEvent:Connect(function()
	for _, foods in pairs(plr.inv:GetChildren())do
		print("Message send")
		wait()
		local clone = CloneTemp:Clone()
		clone.Parent = script.Parent
		clone.Visible = true
		clone.TextButton.Text = foods.Name
	end
end)

this one

Does that script print Message send or not now?
What did you expect it to do?
What children did you expect inv to have?
You need to be more specific, we cannot help you if you just say it doesn’t work.

So. The script didn’t print the message and did not loop
the children in the inv is the tool that the player bought
i made a data store for the inv and the script suppose to loop through all tool that are in the player inv and make a template for each of them.

Still cant find the problem so if any one that see this pls help me

1 Like

Not entirely sure, but I don’t think this is right:

for _, foods in pairs(plr.inv:GetChildren())do

The two variables I see are:

local plr = game.Players.LocalPlayer
local inv = plr:WaitForChild("inv")

you wrote plr.inv:GetChildren() however I don’t think you need to do that as you already defined a variable as inv. just do inv:GetChildren()

I’m not entirely sure this is causing your problem, but it might be.

1 Like

Nope Thats not it :frowning: but thanks for trying to help

i never have this problem with for loop before. idk why i keep reviewing the code over and over again but i cant seem to find any problem :frowning:

Are there things located in the inventory?

Yes there is. The script sometime dose work but sometime it dosen’t

You probably didnt supply a local player when firing the client server

print(#plr.inv:GetChildren())
Put this before the for loop, to check if there actually are any children in inv.