01 | game.ReplicatedStorage.ToggleItem.OnServerEvent:connect( function (player, obj, toggleType) |
02 | if toggleType = = "Equip" then |
03 | for i,v in pairs (player.PlayerGui.Inventory.Background:GetChildren()) do --This is the line |
04 | if string.sub(v.Name, 1 , 5 ) = = "Image" then |
05 | if v.Item.Value = = obj.Name then |
06 | player.Character.Humanoid:EquipTool(obj:Clone()) |
07 | end |
08 | end |
09 | end |
10 | elseif toggleType = = "Unequip" then |
11 | player.Character.Humanoid:UnequipTools() |
12 | end |
13 | 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!)
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;
01 | game.ReplicatedStorage:WaitForChild( "ToggleItem" ).OnServerEvent:connect( function (player, obj, toggleType) |
02 | if toggleType = = "Equip" then |
03 | for i,v in pairs (player.PlayerGui:WaitForChild( "Inventory" ).Background:GetChildren()) do --This is the line |
04 | if string.sub(v.Name, 1 , 5 ) = = "Image" then |
05 | if v.Item.Value = = obj.Name then |
06 | player.Character.Humanoid:EquipTool(obj:Clone()) |
07 | end |
08 | end |
09 | end |
10 | elseif toggleType = = "Unequip" then |
11 | player.Character.Humanoid:UnequipTools() |
12 | end |
13 | end ) |
If this somehow worked then please upvote and accept answer :P.
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.