Template is not a valid member of Script "Script" but it is

I’m making a scrolling frame that displays all players in-game so you can select who to transfer money to. But I’m getting the error "Template is not a valid member of Script “Script” even though it is correctly parented.

image

This is what the code looks like:

local frame = script.Parent
local players = game:GetService("Players"):GetPlayers()
local ttype = Enum.ThumbnailType.HeadShot
local size = Enum.ThumbnailSize.Size420x420


local function update()
	for _, child in ipairs(frame:GetChildren()) do
		child:Destroy()
	end

	for _, player in ipairs(players) do
		local clone = script.Template:Clone()
		clone.Parent = script.Parent
		clone.User.Text = player.Name
		local id = player.UserId
		local content = players:GetUserThumbnailAsync(id, ttype, size)
		clone.Pic.Image = content
	end
end

game:GetService("Players").PlayerAdded:Connect(update)
game:GetService("Players").PlayerRemoving:Connect(update)

update()

Any help is appreciated.

3 Likes

Could you show more of the ancestry of the script?
My suspicion is that the script is running before the template has loaded. Try using WaitForChild to see if this is the issue.

1 Like

image

Wouldn’t this destroy the template? Or am I mistaken?

1 Like

It says there’s a possible Infinite yield when I do WaitForChild.

Your function destroys the Template object as a part of all children.

Give this a shot:

local function update()
	for _, child in ipairs(frame:GetChildren()) do
		if child.Name ~= "Template" then
			child:Destroy()
		end
	end

	for _, player in ipairs(players) do
		local clone = script.Template:Clone()
		clone.Name = "Clone"
		clone.Parent = script.Parent
		clone.User.Text = player.Name
		local id = player.UserId
		local content = players:GetUserThumbnailAsync(id, ttype, size)
		clone.Pic.Image = content
	end
end
2 Likes

Oh, exactly. I think so, the objective is to delete all buttons but the template, thank you.

So the template is being destroyed in that for loop, change it to:


for _, child in ipairs(frame:GetChildren()) do
     if child:IsA("Script") then continue end
     child:Destroy()
end

Edit: this would also work

3 Likes

The script is being destroyed, thus destroying the template too.
The reply above me is the right answer.

With this method, just make sure the cloned template is named anything but Template otherwise nothing with the name Template will be destroyed.

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