Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my script say that my GUI is not a valid member?

game.ReplicatedStorage.ToggleItem.OnServerEvent:connect(function(player, obj, toggleType)
	if toggleType == "Equip" then
		for i,v in pairs(player.PlayerGui.Inventory.Background:GetChildren()) do --This is the line
			if string.sub(v.Name, 1, 5) == "Image" then
				if v.Item.Value == obj.Name then
					player.Character.Humanoid:EquipTool(obj:Clone())
				end
			end
		end
	elseif toggleType == "Unequip" then
		player.Character.Humanoid:UnequipTools()
	end
end)

On the line indicated above in the code, I always get an error. I checked my hierarchy and everything, it is correct. But for some odd reason, it always says 'Inventory is not a valid member of PlayerGui'. Any clue on how to fix this problem? Thanks!

(PS, this is a Server Script!)

2 comments

0

What is the specific error?

0

You do not have the inventory as in PlayerGui. The inventory is a core gui, that is why it says your inventory isen't a valid member.

2 answers

I'm not exactly sure but this has happened once to me. Are you sure there is an GUI called Inventory in the player? If so then I think you need :WaitForChild because it may not have loaded yet.

Here's the edited version of your script with WaitForChild's;

game.ReplicatedStorage:WaitForChild("ToggleItem").OnServerEvent:connect(function(player, obj, toggleType)
    if toggleType == "Equip" then
        for i,v in pairs(player.PlayerGui:WaitForChild("Inventory").Background:GetChildren()) do --This is the line
            if string.sub(v.Name, 1, 5) == "Image" then
                if v.Item.Value == obj.Name then
                    player.Character.Humanoid:EquipTool(obj:Clone())
                end
            end
        end
    elseif toggleType == "Unequip" then
        player.Character.Humanoid:UnequipTools()
    end
end)

If this somehow worked then please upvote and accept answer :P.

2 comments

0

I have no idea, this is the dumb part, it just is an infinite yield ;((((

0

try naming it something else, like InventoryGUI

You do not have the inventory as in PlayerGui. The inventory is a core gui, that is why it says your inventory isen't a valid member. I am not a advanced scripter but when you equip a item it dosen't go to the PlayerGui. Try to look at the wiki.

1 comment

0

No, Backpack is the CoreGui you speak of and it isn't located in PlayerGui, it is located in the Player

This discussion is preserved as it was. New answers, comments, and votes are closed.