Script problem, for _, v in pairs()

  1. What is my goal?
    I want to get the childrens of a ViewportFrame and find the one children who have a child Value named “Level”.

the problem is that the script doesn’t want work

  1. Solutions I tried?
  • look on the dev forum and didn’t find anything
    tried to check back on my script still didn’t find any error

is someone here who can help me?
LocalScript

game.Players.PlayerAdded:Connect(function(player)
	local playerLevel = player.leaderstats:WaitForChild("Level")
	local shop = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent:WaitForChild("Shop")
	local primaryShopButton = shop:WaitForChild("PrimaryShopButton")
	local frameLevel = script.Parent:WaitForChild("FrameLevel")

	primaryShopButton.MouseButton1Click:Connect(function()
		wait(.1)
		for _, v in pairs(script.Parent:GetChildren()) do
			if v:FindFirstChild("Level") then
				
			elseif v.Value >= playerLevel.Value then
				frameLevel.Visible = false
			else
				frameLevel.Visible = true
			end
		end
	end)
end)

Screenshot (174)

1 Like

Do script.Parent.PistolHandle instead

I can do that, but that’s a shop that I am working on and there is more than one item so I need to make a main script for all items.

Screenshot (175)

Instead of GetChildren since it only gets the children, use :GetDescendants(), so it will check every childs child

1 Like

so I change GetChildren for :GetDescendants()?

let me change it I will come back

Yes,

for _, v in pairs(script.Parent:GetDescendants()) do
1 Like

I get into a new problem that the bouton doesn’t want to work.
do you have any idea?
How can I replace all those Parent.Parent.Parent.Parent.Parent… to be sure that the LocalScript can find the Bouton.
it doesn’t print Click

You could try:

primaryShopButton.Activated:Connect(function()
print("Click")
1 Like

still don’t print()
but I change the part of my script with Parent.Parent.Parent.Parent.Parent to game.StarterGui:WaitForChild(“Shop”), like that I am more sure that the LocalScript is finding the bouton

The reason nothing works is because of using PlayerAdded inside of a LocalScript.

Just put the script inside of the StarterPlayer.StarterPlayerScripts, remove the PlayerAdded function and make a new variable called “player” which is game.Players.LocalPlayer

You’ll need to change some variables after that, so make sure that you remove every script.Parent and change them to: player:WaitForChild("PlayerGui"):WaitForChild("Shop").PrimaryShopButton (refer from PlayerGui instead from the script)

1 Like

ok thanks, I will try it now to see if its work

1 Like