• Register a free account to view all forum content. Click here to create your own account.

Problem with :GetChildren() Indexing Wrong Values?

repgainer3

New Member
Joined
Nov 6, 2018
Messages
5
Roblox
repgainer3
Discord
Alright_#5361
I'm really not sure what the issue here is, but my best guess is that when I'm calling :GetChildren(), it's not working. I have this code which is inside of an item on the ground, when clicked it is supposed to set the first available PlayerStats.Items slot to the items item.Value. Right now it will let me pick up 1 item, but then it will think, and even read back to me that all of my inventory slots are full when they aren't, what's wrong here? Also, I'm not getting any errors, the script thinks it's working properly.

Code:
script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local playerItems = player.PlayerStats.Items:GetChildren()
    for i=1,#playerItems do
        print(playerItems.Name, playerItems.Value)
        if playerItems.Value == 0 then
            playerItems.Value = script.Parent.Item.Value
            script.Parent:Destroy()
            break
        end
    end
end)
Post automatically merged:

Oh shoot, I'm sorry, I just saw the error in my code, I'm referencing the items wrong. I'm stating "playerItems.Value" when I should be using "playerItems.Value", this is because playerItems is a table. Sorry
Post automatically merged:

Wait nevermind, I just pasted my code wrong
Post automatically merged:

Here's the real code
Code:
script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local playerItems = player.PlayerStats.Items:GetChildren()
    for i=1,#playerItems do
        print(playerItems[i].Name, playerItems[i].Value)
        if playerItems[i].Value == 0 then
            playerItems[i].Value = script.Parent.Item.Value
            script.Parent:Destroy()
            break
        end
    end
end)
 
Last edited:
Joined
Jun 4, 2018
Messages
240
Ratings
11
Roblox
proclet
My best guess is that you need to send a request to the server via RemoteEvents. You can't do that via a normal script, since all games are now FilterEnabled by default.
 

CallMeKY

Active Member
Joined
Feb 25, 2018
Messages
387
Ratings
115
Roblox
CallMeKY
Discord
CallMeKY#5012
I'm really not sure what the issue here is, but my best guess is that when I'm calling :GetChildren(), it's not working. I have this code which is inside of an item on the ground, when clicked it is supposed to set the first available PlayerStats.Items slot to the items item.Value. Right now it will let me pick up 1 item, but then it will think, and even read back to me that all of my inventory slots are full when they aren't, what's wrong here? Also, I'm not getting any errors, the script thinks it's working properly.

Code:
script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local playerItems = player.PlayerStats.Items:GetChildren()
    for i=1,#playerItems do
        print(playerItems.Name, playerItems.Value)
        if playerItems.Value == 0 then
            playerItems.Value = script.Parent.Item.Value
            script.Parent:Destroy()
            break
        end
    end
end)
Post automatically merged:

Oh shoot, I'm sorry, I just saw the error in my code, I'm referencing the items wrong. I'm stating "playerItems.Value" when I should be using "playerItems.Value", this is because playerItems is a table. Sorry
Post automatically merged:

Wait nevermind, I just pasted my code wrong
Post automatically merged:

Here's the real code
Code:
script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local playerItems = player.PlayerStats.Items:GetChildren()
    for i=1,#playerItems do
        print(playerItems[i].Name, playerItems[i].Value)
        if playerItems[i].Value == 0 then
            playerItems[i].Value = script.Parent.Item.Value
            script.Parent:Destroy()
            break
        end
    end
end)
I'm not sure what the problem is. It looks like you have a value set to zero to represent the player having no item, and when you click something, it changes the value to show the player does have an item now? I recreated this slightly different and it worked as intended.
Code:
-- test setup

local ClickPart = Instance.new('Part', workspace)
local Detector = Instance.new('ClickDetector', ClickPart)
local PartItem = Instance.new('IntValue', ClickPart)
PartItem.Name = 'Item'
PartItem.Value = math.random(1, 100)

game.Players.PlayerAdded:Connect(function(Player)
    local PlayerStats = Instance.new('Folder', Player)
    PlayerStats.Name = 'PlayerStats'
    local Items = Instance.new('Folder', PlayerStats)
    Items.Name = 'Items'
    for i = 1, 10 do
        local newItemSlot = Instance.new('IntValue', Items)
        newItemSlot.Name = 'Item' .. i
        newItemSlot.Value = 0
    end
end)



-- actual code

Detector.MouseClick:Connect(function(Player)
    for _, Item in pairs(Player.PlayerStats.Items:GetChildren()) do
        print(Item.Name .. ': ' .. Item.Value)
        if Item.Value == 0 then
            print('Slot available!')
            Item.Value = PartItem.Value
            break
        end
    end
end)