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

:GetChildren() "#" not working as intended?

This script doesn't print success once, I don't know what the issue is? "Players Ready" is a text label, it is a local script

game.Players.PlayerAdded:Connect(function(plr)
plr:WaitForChild("PlayerGui"):WaitForChild("Game"):WaitForChild("Frame"):WaitForChild("Night"):WaitForChild("PlayersReady").Changed:Connect(function()
    local ign = game.ReplicatedStorage.InGameNames:GetChildren()

if #ign > 0 then 
    print("Success!")
    if plr:WaitForChild("PlayerGui"):WaitForChild("Game"):WaitForChild("Frame"):WaitForChild("Night"):WaitForChild("PlayersReady").Text == tostring(#ign) then
        print("Success!")
            game.ReplicatedStorage.RemoteEvents.DayNightCycle:FireServer()
        end
    end
end)
end)


3 comments

0

"ign" is the amount of children in InGameNames. Maybe it ISN'T greater than 0?

0

This script fires when the"Players Ready" text label is changed, there is a button that you press to say you're ready, when you press it, a local script updates the "Players Ready" text label to + 1, this works, you cannot press the players ready button if you are not in the game, when you are in the game, a value is created inside of theInGameNames

0

Also if I change it to | if #ign >= 0 then | it still doesn't print "Success!"

2 answers

@sweettart13947 I tried to make it work but it didn't work, this is my modified script with your addition, this modified script doesn't work.

game.Players.PlayerAdded:Connect(function(plr)
plr:WaitForChild("PlayerGui"):WaitForChild("Game"):WaitForChild("Frame"):WaitForChild("Night"):WaitForChild("PlayersReady").Changed:Connect(function()
	local ign = game.ReplicatedStorage.InGameNames:GetChildren()
	local function CountElement(Table)
    local Count = 0;
    for i,v in next, Table do
        Count = Count + 1
    end
    return Count
end
CountElement(game.ReplicatedStorage.InGameNames:GetChildren())
if #ign > 0 then 
	print("Success!")
	if plr:WaitForChild("PlayerGui"):WaitForChild("Game"):WaitForChild("Frame"):WaitForChild("Night"):WaitForChild("PlayersReady").Text == tostring(#ign) then
		print("Success!")
			game.ReplicatedStorage.RemoteEvents.DayNightCycle:FireServer()
		end
	end
end)
end)



1 comment

0

No, I will make a reply to this in a second. You're using it incorrectly.

@zompi

Your implementation of my script is incorrect. Due to Lua's wacky "#" syntax, sometimes it doesn't count a table properly. So I made a function to fix this.

This is a fixed version of your script:

local function CountElements(Table)
    local Count = 0;
    for i,v in next, Table do
        Count = Count + 1
    end
    return Count
end

game.Players.PlayerAdded:Connect(function(plr)
    plr:WaitForChild("PlayerGui"):WaitForChild("Game"):WaitForChild("Frame"):WaitForChild("Night"):WaitForChild("PlayersReady").Changed:Connect(function()
        local Length = CountElements(game.ReplicatedStorage.InGameNames:GetChildren())
        if Length > 0 then 
            print("Success!")
            if plr:WaitForChild("PlayerGui"):WaitForChild("Game"):WaitForChild("Frame"):WaitForChild("Night"):WaitForChild("PlayersReady").Text == tostring(#ign) then
                print("Success!")
                game.ReplicatedStorage.RemoteEvents.DayNightCycle:FireServer()
            end
        end
    end)
end)

If this doesn't work then it is something with your game.

3 comments

0

It still doesn't even print the first success, nothing is wrong with my game, there is 1 child in "InGameNames" and the "PlayersReady" text is 1 therefore it should print ("Success!"), is it due to the fact that this is a local script? )

0

Oh wait, I see. PlayerAdded is not being called because you join before your local script loads. I recommend you put this in a script (but replace the :FireServer() with a function)

0

@sweettart13947 I added "if script then" on the first line of my original script but it didn't work, I don't understand what you mean by replace fire server with a function

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