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

Remove GUI if person walks too far away from part?

1 answer

✓ Accepted answer

I'm not going to just give you the script however i will give you a line of code that checks if the character's torso is within a certain stud radius

script:

local Radius = 100 --in studs
local Part = (Part Location Here)
local RS = game:GetService("RunService")
local InRange = False

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		local connection = RS.Heartbeat:connect(function() --constantly checking
			if (char:WaitForChild("HumanoidRootPart").Position - Part.Position).magnitude < Radius then
				InRange = true
				--insert code to toggle gui off here.
			else
				InRange = false
			end
		end)
		if InRange == false then
			connection:Disconnect()
		end
	end)
end)

Line 9 of code essentially takes the HRP's pos and the Part's pos' difference and uses an already built in equation to return the magnitude, and you can set a boundary by using if statements.

due to snobby developers here's a better and more improved version

28 comments

2

Explain the code. Also don't use while wait() do and connect is deprecated.

0

If you would let me edit in time then that would've been better. @incapaxx

0

That's your fault for not explaining in the first place and don't explain in the comments. It makes the code messy. Also as I said while wait() do is bad code and :connect is deprecated (use :Connect)

0

"I'm not going to give script" > Gives script.

2

Yeah uh, I didn't want a whole script I just wanted information like properties and such.

0

@Phlegethon5778 i gave the main basis script, @incapaxx :connect is makes not difference, built-in functions can be called with lower case as the first letter. FE: model:findFirstChild()

0

But they're deprecated. Read here on why not to use deprecated items https://developer.roblox.com/articles/Deprecation

0

If you are just going to criticize my response then maybe you should put more effort into your responses @incapaxx at least I'm making an attempt.

0

what lol i do put efforts. well your attempt sucks and it's just teaching bad code.

1

No response is better than a poor, uninformed answer with bad practices and deprecated code.

0

@incapaxx deprecated or not it makes no difference and I also revised my code thank you very much.

0

That huge comment is ticking me off I'm moving it off the script. It will improve your codes readability as well.

0

My comment under the question is worth more than two times the value of your answer. Effort takes different forms and some effort is better than others.

0

@Phlegethon5778 so what am i just supposed to post a link and be lazy like you?

0

that's not laziness xd and the OP even said "Yeah uh, Indidnt want a whole script I just wanted information like properties and such."

0

I posted a link because other people have already put in quality work on this question.

0

'Hey, I am having a hard time figuring out how to close or open a GUI if a player gets within a certain radius of a part or walks away. Would appreciate some help.' no do i see that, That's his downfall for announcing that afterwards.

0

Laziness is doing poor or no work when good effort is required. In my opinion, you're the one who is lazy.

0

I was just efficient and good. You were inefficient and bad.

0

Also, it's against the rules of the website to post answers with no explanation.

0

@Phlegethon all you did was copy and paste, i actually took the time to write out code. in technical terms this situation is reversed. but by your standards i guess you're right if it makes you happy.

0

Would you consider a boy who took out the trash and spilled it all over the house lazy? Or, would you call the boy who controlled a robot from his room to do it for him that also included features like replacing bags and cleaning the trash cans lazy? One did more work, but he did it in a way that made him the lazy one.

0

You're the boy who spilled the trash all over the floor.

0

And uh, please calm down.

0

ok since you wanna go there: Would you consider someone walking/jogging lazy or the person riding a segway more productive than the person walking/jogging.

0

ok since you wanna go there: Would you consider someone walking/jogging lazy or the person riding a segway more productive than the person walking/jogging. (You're the person riding the segway)

0

Oh my god stop.

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