Hello, I'm trying to make it so that when a player goes near the NPC, a gui shows up, this is the script i have so far:
Guest = script.Parent Ggui = script.Parent.BillboardGui local player = game.StarterPlayer.StarterCharacter while true do for i,v in pairs(player:GetChildren()) do --Gives Table of parts inside of "player" if (v.HumanoidRootPart.Positon.Magnitude == Guest.HumanoidRootPart.Position.Magnitude)<= 10 then Guest.BillboardGui.ImageButton.Visible = true else Guest.BillboardGui.ImageButton.Visible = false end wait(0.5) end end
But when I do this I get an error message saying, "HumanoidRootPart is not a valid member of part". Does anyone know whats wrong with the code?
On line 3, you are attempting to access game.StarterPlayer, this does not find the player, this is what is used to add into the player when they join the game/respawn ("StarterCharacter" is not exactly a thing either). So since this should be a local script, use game.Players.LocalPlayer.Character. LocalPlayer finds the player who uses the GUI, and Character will reference the Player >model< inside the game.Workspace.
You do not require an iterator loop on line 6 (for i, v in pairs(x)) as you are only trying to access one part (HumanoidRootPart), and can be specified with a simple directory.
magnitude is a property of vector values, works as a way of finding the length of a vector, (equates to Pythagoras' Theorem), so for any value classified as a vector, add a ".magnitude" to find it's "length". Of course, we must first subtract the two vectors, because the distance will be based around the origin of the grid they use. (Line 7 of OP's code)
Misc: Typo occurred in line 7 "Positon"; Lines 8 & 10 "Guest.BillboardGui" simplified to "Ggui".
Here is what should be a simplified version of the code..
Guest = script.Parent Ggui = script.Parent.BillboardGui local player = game.Player.LocalPlayer.CharacterAdded:wait() -- player model found in the workspace while wait(0.5) do if (player.HumanoidRootPart.Position - Guest.HumanoidRootPart.Position).magnitude <= 10 then Ggui.ImageButton.Visible = true else Ggui.ImageButton.Visible = false end end
EDIT: The player's character does not immediately create, so I have modified the code to account for that (line 3; credit to user alphawolvess).
EDIT2: Localscripts will not run when parented under "Guest" .. this must be placed in game.StarterPlayer.StarterPlayerScripts and specify the NPC from there.. ask me if any more problems arise.