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)
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.
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)
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
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)
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
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)