Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is this code that reads a textbox not working properly?

Asked by
pevdn 32
2 years ago
Edited 2 years ago
local players = game.Players
local rblxCode

game:WaitForChild("Players").PlayerAdded:Connect(function(plr)
    rblxCode = plr.PlayerGui["rblx.basic"].Frame.TextBox.ContentText
    if rblxCode == 'Print("Hello, world!")' then
        print("Hello, world!")
    end
end)

How come this code would not work? When I join the game, it outputs the error: rblx.basic is not a valid member of PlayerGui "Players.pevdn.PlayerGui" Even though it definitely is. https://ibb.co/zFzc9YJ

1 answer

Log in to vote
0
Answered by
DevNetx 250 Moderation Voter
2 years ago

You're trying to index the ScreenGui when the player joins, and it probably doesn't exist at that point.

You should use :WaitForChild() to make sure it definitely exists first. For example:

local gui = plr.PlayerGui:WaitForChild('rblx.basic')
Ad

Answer this question