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

How do I fix a error for GUI "nil value" for a name?

Asked by 5 years ago

So, I'm trying to make a GUI appear for one player. I had an idea to go into the player folder under explorer, find the players name who clicked the GUI, and open the GUI for them. I keep on getting a error. Any help?

local name = plr.Character.Name
Button2.MouseButton1Down:connect(function()
game.Players.name.PlayerGui.TrailGUI.Main.Visible = true
end)

Error: Workspace.TrailGUI:151: attempt to index field 'Character' (a nil value)

1 answer

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Perhaps the character hasn't loaded yet at runtime. You want to yield to make sure that the character exists before proceeding using CharacterAdded:Wait(). Also, what you've done on line 3 won't work because saying game.Players.name will look for a name property of game.Players, it won't look for the value of name.

Here's what you can do instead:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local playerGui = player:WaitForChild("PlayerGui")
local trailGui = playerGui:WaitForChild("TrailGUI")
local main = trailGui:WaitForChild("Main")

Button2.MouseButton1Down:Connect(function() -- changed connect to Connect because connect is deprecated
    main.Visible = true
end)

Hope this helps! :)

0
Thank you soooo much! IPhoneDrew 9 — 5y
Ad

Answer this question