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

Why does this not find the player and his playergui?

script.Parent.MouseButton1Click:connect(function(bounty)
	local player = game.Players.LocalPlayer
	local selectedplayer = game.Players:findFirstChild(player.PlayerGui.Teammates.Frame.TextBox.Text)
	selectedplayer.PlayerGui.Team.Frame.Visible = true 
	selectedplayer.PlayerGui.Team.Frame.Who.Text = selectedplayer.Name	
end)

I get the error playergui is not a valid member of player.. I dont know what Im doing wrong?

1 answer

The issue is that you're assuming that game.Players:findFirstChild(player.PlayerGui.Teammates.Frame.TextBox.Text) is going to always return a player. If the Text in the TextBox doesn't exactly match a Player's name, however, this will be nil, so that selectedPlayer.PlayerGui is the same as saying nil.PlayerGui.

To fix, check first:

script.Parent.MouseButton1Click:connect(function(bounty)
    local player = game.Players.LocalPlayer
    local selectedplayer = game.Players:findFirstChild(player.PlayerGui.Teammates.Frame.TextBox.Text)
    if (selectedplayer) then
        selectedplayer.PlayerGui.Team.Frame.Visible = true 
        selectedplayer.PlayerGui.Team.Frame.Who.Text = selectedplayer.Name
    else
        warn("Couldn't find player with that name")
    end
end)

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