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

Why is the player nil when I call a GUI?

When I try to call a GUI, the output's nil, but when I try to print something it works. Here is the code:

p = script.Parent.Position
range = 500-- the range
frame = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame

while true do
    wait(.1)
    plrs = game.Players:GetChildren()
    for i,plr in ipairs(plrs) do
        if plr.Character ~= nil then
            tor = plr.Character.UpperTorso
            if (p - tor.Position).magnitude <= range then
				print("yay") 
               frame.Visible = true
			else
				frame.Visible = false 
            end
        end
    end
end

Thanks for the Help!

1 answer

✓ Accepted answer

Your problem is that the player doesn't load in as quickly as scripts, so when the script runs, the player will still be unloaded, aka nil.

To solve this, make a variable for the player, and then reference it where you need to, like so:

local player = game.Players.LocalPlayer or wait() --Checks for player, then if they are not there, wait.
local frame = player.PlayerGui.ScreenGui.Frame --It's also good practice to use local variables.

If this solved your problem, I'd appreciate my answer accepted or upvoted; hope I helped!

2 comments

0

why does it give me this: attempt to index local 'player' (a number value)?

0

nevermind, I found my error, Thanks for the Help!

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