For loop issue, not progressing code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = ReplicatedStorage:WaitForChild("Tools")
local CraftTool = ReplicatedStorage:WaitForChild("CraftTool")
local craftingInfo = require(ReplicatedStorage:WaitForChild("CraftingInfo"))

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CraftTool = ReplicatedStorage:WaitForChild("CraftTool")

local CraftingFrame = script.Parent

for i, button in pairs(CraftingFrame:GetChildren()) do
	if button:IsA("TextButton") then
		button.MouseButton1Up:Connect(function()
			CraftTool:InvokeServer(button.Name)
			print("print1")
		end)
	end
end

This code is under StarterGui.ScreenGui.Frame.LocalScript
The remote function referenced is under ReplicatedStorage
If it’s relevant, here is the received code:

CraftTool.OnServerInvoke = function(player, toolName)
local leaderstats = player.leaderstats
	local crafted = false
	
	for i, v in pairs(craftingInfo[toolName]) do
		local material = leaderstats:FindFirstChild(i)
print("pleaseprint")
		if material then 
			print("print2")

print1 and pleaseprint prints but not print2

Edited after narrowing problem down to for loop instead of invoke.

2 Likes

Does it work if you use ``if leaderstats:FindFirstChild(i) then? If it doesn't put an else on the if material then` and tell it to print every child of leaderstats.
Example:

if material then
	print("print2")
else
for _, i in pairs(leaderstats:GetChildren()) do
	print(i)
end

what are i and v in your for loop? try outputting both
did you make sure that FindFirstChild should be using i rather than v? and did you make sure whatever i is matches the name of something in leaderstats?

1 Like

Oh, it’s because you’re trying to find a child with the number. (Which is i)
The second variable in pairs is the name, the first is a number.

because it uses pairs(), the first isnt necessarily a number
it would be a string in { color = "red" }
@UltraConstructor6 didnt reveal what’s inside craftingInfo though, so we can’t tell from here

Thanks so much for your help. I admit I followed a tutorial and customized it to fit my game, so I’m not sure what the for loop means.

local craftingInfo = {
	
	["Wood Sword"] = {
		["Wood"] = 3;
	};
	
	["Throwing Rocks"] = {
		["Rock"] = 5;
	};
	
	["Slingshot"] = {
		["Wood"] = 2;
		["Rock"] = 1;
	};
	
	["Stone Spear"] = {
		["Wood"] = 3;
		["Rock"] = 4;
	};
	
	["Sword"] = {
		["Wood"] = 1;
		["Rock"] = 1;
		["Metal"] = 2;
	};
	
	["Metal Bat"] = {
		["Metal"] = 3;
	};
	
	["Pike"] = {
		["Wood"] = 4;
		["Metal"] = 3;
	};
}

return craftingInfo

craftingInfo is a modulescript under replicatedstorage that contains the names and requirements for the craftable items.

The main thing I’m confused about, is how the index for the for loop (i), can be used with FindFirstChild, which I always thought only used strings?

i in that case would be values like “Wood” and “Rock”, so I think you properly used FindFirstChild(i)

now maybe the issue is that you don’t have the leaderstats named Wood and Rock

Should I be able to view leaderstats as a folder in the explorer?
On game start I search for it and It’s not there.

YOU’RE RIGHT! I dont think those 3 exist in leaderstats yet, I need to clone them over.

1 Like