Magnitude Problem

Hello,
First time messing with ‘magnitude’ and I’ve come across a bump in the road. I’ve created a system where if the player is in a certain radius of a door, a gui will pop up. Then, whenever the player is outside of the radius, the gui will disappear.

It all works fine, but the issue is figuring out a way to only run the function once possibly using some sort of ‘changed’ event. At the moment I’m using a ‘while’ loop and it’s not giving the desired effects.

Here’s what the ‘while’ loop does whenever a player is inside of the radius:
https://gyazo.com/3ebec9593ddcfe7afa60469b649da25b

As you can see, it’s flashing because it’s constantly calling the function. I’m sure it’s a simple fix and I’m just having a brain fart, but I don’t know where to begin. I’d appreciate if someone can tell me what to look up or do, instead of some code.

EDIT: Oops, forgot the code, sorry:

local player = game.Players.LocalPlayer
local phealth = player.Character:WaitForChild("Humanoid").Health
local doorpart = game.Workspace.door.doorPart
local guiPart = game.Workspace.partGui	
local tweenservice = game:GetService("TweenService")
local guipartPos = guiPart.Position

--- Main Code ---

local radius = 10

while true do
	wait(1)
	local dist = (player.Character:FindFirstChild("Torso").Position - doorpart.Position).magnitude
	if dist <= radius then
		local goal = {}
		goal.Position = guipartPos + Vector3.new(0,0.75,0)
		local tweenInfo = TweenInfo.new(0.8)
		local tween = tweenservice:Create(guiPart, tweenInfo, goal)

		tween:Play()
		for i = 1, 0, -0.1 do
			guiPart.SurfaceGui.Frame.enterButton.BackgroundTransparency = i
			guiPart.SurfaceGui.Frame.enterButton.TextTransparency = i
			wait(0.005)
		end
		print("Player is near door")
	else
		print("Player is no longer near door")
	end
end
``

One way you might be able to it is to add some boolen logic( kind of like a Debounce)

local CanActivate  = true
while true do
	wait(1)
	local dist = (player.Character:FindFirstChild("Torso").Position - doorpart.Position).magnitude
	if dist <= radius and CanActivate then
   CanActivate = false --- Set CanActivate to false, so that we cant run the code under this twice/again until the player is no longer near the door
		local goal = {}
		goal.Position = guipartPos + Vector3.new(0,0.75,0)
		local tweenInfo = TweenInfo.new(0.8)
		local tween = tweenservice:Create(guiPart, tweenInfo, goal)

		tween:Play()
		for i = 1, 0, -0.1 do
			guiPart.SurfaceGui.Frame.enterButton.BackgroundTransparency = i
			guiPart.SurfaceGui.Frame.enterButton.TextTransparency = i
			wait(0.005)
		end
		print("Player is near door")
    
	else
    CanActivate = true  ---- once the player is no longer in range then we can set CanActivate to true, thus allowing the gui to tween again 
		print("Player is no longer near door")
	end

(Also i like your Gui animation/tween, it looks clean)

2 Likes

Yep that worked, appreciate the help I’ll look into debounces a little more

Also thanks for compliment :smiley:

Hey! i have a code similar to hes code, i want the animation to play only once when i am inside a magnitude, and then play again if i enter that magnitude again.

since you solved hes problem, mind checking out mine?

local RunScervice = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")

local CanActivate  = true
local PlayerHasLeft = false


local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local Closest
local EClip = script.Parent.Parent

local Camera = workspace.CurrentCamera

local eParts = game.Workspace.EClothesFolder:GetChildren()

function InputBegan(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		
		
	end
end


function RenderStepped()
	
	local PlayerPosition = Character.PrimaryPart.Position
	local Closest
	
	script.Parent.Visible = false

	------------------III COLORS III-------------------
	
	
	local Red = game.Workspace.EClothesFolder.Red

	if Red.BrickColor == BrickColor.new("Earth green") then

		script.Parent.ImageColor3 = Color3.fromRGB(255,0,0)

	elseif Red.BrickColor == BrickColor.new("Really red") then

		script.Parent.ImageColor3 = Color3.fromRGB(0, 196, 255)

	end
	------------------III COLORS III-------------------
	
	for i , v in pairs(eParts) do
		if Closest == nil then
			Closest = v
		else
			if (PlayerPosition - v.Position).magnitude < (Closest.Position - PlayerPosition).magnitude then
				Closest = v
			end
		end
	end
	
	if Closest ~= nil then
		for i , v in pairs(eParts) do
			if v == Closest then
				v.BrickColor = BrickColor.new("Earth green")
			else
				v.BrickColor = BrickColor.new("Really red")
			end
		end
		
	end
	
	

	for i , v in pairs(eParts) do
		
		local Mag = (v.Position - HumanoidRootPart.Position).magnitude
		
		
		if Mag <= 6 and CanActivate then
			
		CanActivate = false
			
			EClip:TweenSize(UDim2.new(0, 120,0, 53),"Out","Bounce", .5, true)	

			print("Player is near Clothes")
		else
			--CanActivate = true
			--print("Player is no longer near Clothes")
			
		end
		
		if Mag <= 6 then
			local WorldToScreenPoint = Camera:WorldToScreenPoint(v.Position)
			local Position = UDim2.new(0, WorldToScreenPoint.X, 0,WorldToScreenPoint.Y, 0)
			
			script.Parent.Parent.Position = Position
			
			script.Parent.Visible = true
			
		end
		
	end	
end


UIS.InputBegan:Connect(InputBegan)
RunScervice.RenderStepped:Connect(RenderStepped)