No Errors / Red or Blue Lines but my code won't work

I don’t understand why my code won’t work. I get no forms of errors (output, underlines, etc) but it still won’t work. The code is supposed the make the npc change into a random player on the users friends list.

local usernames = {}
local friends = {}

local function iterPageItems(friendPages)
	for item, pageNo in iterPageItems(friendPages) do
		table.insert(usernames, item.Username)
		table.insert(friends, item.Id)
	end

	local friends = {}
	for item, pageNo in iterPageItems(friendPages) do
		table.insert(friends, item.Id)
	end

	local randomFriend = friends[math.random(#friends)]

	local npc = game.Workspace.npc
	local newHumanoidDescription = game.Players:GetHumanoidDescriptionFromUserId(randomFriend)
	npc.Humanoid:ApplyDescription(newHumanoidDescription)
end

What are you trying to do with this? You’re calling the function inside of itself, it’s going to loop infinitely. Are you trying to make a custom iterator function?

No thats what confused me. I’m sorta a mid scripter and when I posted my script in the Development-Help channel in the Roblox discord, someone said I had to Define iterPageItems and I thought I did it right. How would I fix that? Is that the only bug?

Is this a LocalScript?

If so, where is it located?

it is a localscript located in workspace.

Thats your issue, LocalScripts do not work in the workspace

where do I put it in if not workspace?

There a Couple of Places:

  • ReplicatedFirst

  • StarterGui

  • StarterPlayerScripts

  • StarterCharacterScripts

Still didn’t work. I moved it to StarterPlayerScripts

Are you even calling the function in the code?

you must have misunderstood, they meant “iterPageItems” is not a built in function so you have to define what you want it to do elsewhere, not put everything you made in it

how would I define that? I’m confused on how to do it right.