Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make an event fire when a part enters a radius?

Asked by 8 years ago

You see, what I'm trying to do is make a gui open when a player's torso comes within a radius around another part. Trust me, I'd love to have some code to show you, but I don't even know where to begin.

2 answers

Log in to vote
0
Answered by
legosweat 334 Moderation Voter
8 years ago

Using a while loop may not be efficient, but I am going to use it in this example. The script is going to be local, but I don't know if you want it to be local or server script. But, for now it's going to be local.

The radius between two parts can be processed by using Magnitude http://wiki.roblox.com/index.php?title=Magnitude

LOCAL SCRIPT:

local Item = game.Workspace.Whatever
local Player = game.Players.Localplayer
local Character = Player.Character

while wait() do
 -- Has to be less than 60 studs away for the event to fire: 
if (Item.Position - Character.Head.Position).Magnitude < 60 then
-- Event Fires Here; Lets say there's a GUI, you'd want to make it visible it here.
else
-- Event Stops Here; Then you'd want to make it invisible here.
end
end

This should get you on a good start.

Ad
Log in to vote
0
Answered by
theCJarmy7 1293 Moderation Voter
8 years ago

for this you would use magnitude

magnitude is the distance between two parts, and can be found like this:

local magnitude = (game.Players.LocalPlayer.Character.Torso.Position - game.Workspace.OtherPart.Position).magnitude -- MAKE SURE THIS IS LOCALSCRIPT! It's important enough for the caps!

now, magnitude the variable contains the players distance from OtherPart

as for the gui, that can be done like this:

local magnitude = (game.Players.LocalPlayer.Character.Torso.Position - game.Workspace.OtherPart.Position).magnitude
if magnitude >= 10 then -- change 10 to whatever you want the distance to be
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true -- change the frame to whatever
end

but it will only do this once, so we need it to repeat!

while wait() do 
local magnitude = (game.Players.LocalPlayer.Character.Torso.Position - game.Workspace.OtherPart.Position).magnitude
if magnitude <= 10 then -- change 10 to whatever you want the distance to be
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true -- change the frame to whatever
end
end

I hope I helped!

Answer this question