Problem with GUI Magnitude code

Any idea on what’s causing the script not to run? Someone and I have been trying to figure it out what’s causing it not to run for almost an hour now. Thanks.

local GUI = script.Parent
local open = GUI:WaitForChild("FrameOpen").Open
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local PressE = game.Workspace:WaitForChild("PressE")
local Distance = (Root.Position - PressE.Position).Magnitude

open.MouseButton1Click:Connect(function()
print ("Distance")
if Distance < 15 then
print("Distance is less than 15!")
GUI.Enabled = true
end
end)
2 Likes

Hi! By not running, do you mean the script itself isn’t executing?

If this is the case, could you tell me whereabouts the script is located? As it seems like you’re using a LocalScript, they don’t run anywhere on the server side.

If the script is technically executing, perhaps try using various print statements around the code to identify which variables in which state and to try and identify exactly where the script is going wrong.

For future reference, printing things out to find out what’s happening is a nice way of tracking and dry-running your code without the hassle of using watches.

Hope this helps!

2 Likes

It’s a local script, also it’s located inside a ScreenGui.

If the gui moves, then you have to put the Distance variable IN the actual code:

open.MouseButton1Click:Connect(function()
local Distance = (Root.Position - PressE.Position).Magnitude
print ("Distance")
if Distance < 15 then
print("Distance is less than 15!")
GUI.Enabled = true
end
end)

(and it’s probably better to use AbsolutePosition)

1 Like

Distance will always remain constant in the script, so you’ll have to get the new distance when the button is press:

-- in the connection's function
local Distance = (Root.Position - PressE.Position).Magnitude
if Distance < 15 then
    Gui.Enabled = true
end

Upon execution, the script will get the player’s current distance from PressE and store it in a variable and will never be changed. If the recorded distance was 50 studs and the player pressed the button, even if the player is within 15 studs from PressE, the distance will always be 50

1 Like

The GUI doesn’t move, what I’m trying to make happen is when ur in the magnitude area, make the GUI appear.

Wait, I just responded to the same post…

Don’t post threads twice

1 Like

Not the same thread, similar problem different thread.

Here is the fixed code you should use Visible property on Frame only to make gui appear. Example:

local GUI = script.Parent
local open = GUI:FindFirstChild("FrameOpen").Open
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character:FindFirstChild("HumanoidRootPart")
local PressE = game.Workspace:FindFirstChild("PressE")

open.MouseButton1Click:Connect(function()
 local Distance = (Root.Position - PressE.Position).Magnitude
 print ("Distance")
  if Distance < 15 then
   print("Distance is less than 15!")
   GUI.Enabled = true -- Find Frame in ScreenGui
   -- or
   -- Frame.Visible = true
  end
end)
1 Like

They’re not exact duplicates - in the previous post, the OP asked for help on how to check the distance. He got the code, but he had problems with it, hence why he created a new one

But @MegaTheMagnificent, you could’ve just posted on the same thread asking for help

1 Like

That was your question for the other thread

that’s your question in this post

Not to be mean, but, you’re not allowed to post threads twice

1 Like

I guess it is similar, whatever. It’s just two post lmao, it’s not like I be spamming.

Still, not working oof.

local GUI = script.Parent
local open = GUI:WaitForChild("FrameOpen").Open
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local PressE = game.Workspace:FindFirstChild("PressE")

open.MouseButton1Click:Connect(function()
local Distance = (Root.Position - PressE.Position).Magnitude
print ("Distance")
if Distance < 15 then
print("Distance is less than 15!")
game.StarterGui.OpenGui.Frame.Visible = true
end
end)

Why do you use WaitForChild if GUI and player is already in game? You should only use when changing parent of an Instance, object,… because it comes later in different place or position. Also you have to use PlayerGui instead of just calling StarterGui because else the change is not going to be visible on client-side. Here is the fix:

local GUI = script.Parent
local open = GUI:FindFirstChild("FrameOpen").Open
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character:FindFirstChild("HumanoidRootPart")
local PressE = game.Workspace:FindFirstChild("PressE")

open.MouseButton1Click:Connect(function()
   local Distance = (Root.Position - PressE.Position).Magnitude
   print ("Distance")
if Distance < 15 then
   print("Distance is less than 15!")
   Player.PlayerGui.OpenGui.Frame.Visible = true
  end
end)

In the output it says, Argument 1 missing nil? I’m pretty new to scripting so don’t know a whole bunch yet.

We don’t use any arguments here? You sure this is an error from this script?

It sends me to the second line of code. Nvm, it had two brackets. :man_facepalming:

Where should I put my localscript?

Players.MegaTheMagnificent.PlayerGui.OpenGui.FrameOpen.GuiScript:2: attempt to index a nil value?

Do you have any Button named Open inside FrameOpen thing?