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

Why is my gui not appearing when i come close to a part?

So, i recently learned about magnitude. I wanted to challenge myself, but it does not work considering I've used a long time on it.

There is an error: Players.maxpax2009.PlayerScripts.ShowGUI:4: attempt to index nil with 'FindFirstChild'

This is a local script by the way!

Here's the Local Script:

local Player = game.Players.LocalPlayer
local AggroDist = 10
local Character = game.Workspace:FindFirstChild(Player.Parent)
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")

local GUI = game.Workspace.Cannon.GUIShow
if (HumanoidRootPart.Position - game.Workspace.Cannon.GUIShow.Position).magnitude < AggroDist then
	GUI.BillboardGui.Enabled = true
end

Thanks for taking your time

1 comment

0

Question: why are you searching game.Workspace for the player's parent (game.Players)? I'd say change line 3 to game.Workspace:FindFirstChild(Player.Name)

1 answer

Character is a property of Player, the issue was that you tried to look for game.Players in workspace (the parent of Player is game.Players).

local Player = game:GetService('Players').LocalPlayer
repeat wait() until Player and Player.Character and Player.Character:FindFirstChild('HumanoidRootPart')
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart

local AggroDist = 10

local GUI = game.Workspace.Cannon.GUIShow
if (HumanoidRootPart.Position - game.Workspace.Cannon.GUIShow.Position).magnitude < AggroDist then
    GUI.BillboardGui.Enabled = true
end

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