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

How can i detect a humanoid that has stepped into an area?

Asked by
Sorukan 240 Moderation Voter
5 years ago
Edited 5 years ago

I've looked through many pages and i couldn't find the answer. I've seen many people suggesting the use of Magnitude but i don't understand how that would work because magnitude only represents the distance between two points in a straight line so if i want to create a circular area then i don't see how that could work out. I'm trying to make it so that within that area, all humanoids that are inside it will take damage so using the touched event won't work.

0
why wouldnt touched work? DinozCreates 1070 — 5y
0
it wouldn't work if i want to damage all the humanoids within that area all at once because the touched event only works for one part at a time Sorukan 240 — 5y
0
No, the Touched Event could still work, just get the Hit.Parent.Parent, and then you can find the other Players, and their humanoid. TheOnlySmarts 233 — 5y
0
What i did was Hit.Parent:FindFirstChild("Humanoid") doing Hit.Parent.Parent will get the player but what's the point? Sorukan 240 — 5y
View all comments (3 more)
2
"A circular area" is in fact created by a distance function: you can test whether or not a point P is inside a sphere/circle by checking for whether Magnitude(P - EffectCenter) <= EffectRadius. aschepler 135 — 5y
1
I don't think i understand magnitude well enough, i'll look into it a bit more, any links would be appreciated. Sorukan 240 — 5y

1 answer

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

I don't see why a .touched event won't work? and you can aswell use magnitude for this, it will be harder. Since a touched event is easy to figure out by yourself, i'll be explaining you how to use magnitude for this.

magnitude is a distance between 2 points, those 2 points are our 2 parts in studio, in normal maths the magnitude or distance between two points is the root of (vec.x + vec.y + vec.z) so in lua you gotta do

local part1 = workspace.Part1 --or where ever your part is
local part2 = workspace.Part2 --or where ever your part is

local distance = (part1.Position - part2.Position).magnitude

there we go, thats pretty much how we use magnitude

now, if you wanna use this for your area checking, we pretty much have to choose our 1st part (it can be anything but i guess humanoidrootpart will be our best bet) and our 2nd part, and that can be an invisible and non concolided part, its just there to act up like a 2nd part so we have a distance to check. now, after we our first magnitude, we gotta find the distance that our 1st part has cant pass, or it will be damaged. for that we can just calculate that distance, by making another part near the edge of circle/sphere. and just a remeinde to get this magnitude you gotta

local part1 = workspace.Part1 
local part2 = workspace.Part2

print((part1.Position - part2.Position).magnitude)

and now we got our distance in the output! now you can just remove this part and the script for it since its just for testing, and now we compare our 2 distances that we got!

and that should be it, this is how youre script is probarly going to look like, (this is a local script btw)

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local part1 = char:WaitForChild("HumanoidRootPart", 0.1)
local part2 = workspace.Part2 -- or where ever your second part is

local dist = (part1.Position - part2.Position).magnitude
local maxdist = 10.1245235112 -- this is the distance that we calculated and the distance we dont want humanoid to pass

if dist <= maxdist then
    print("Humanoid is terrapassing!!")
end

and that should be it, this isnt an efficent way, but my purpose is just to explain how this works!

0
btw our first part gotta be one of the player's bodyparts starmaq 1290 — 5y
0
I understand it but the example you gave doesn't seem to be working. Sorukan 240 — 5y
0
yah sorry, i was sure this is was hard to do, but i guess you know how magnitude works! starmaq 1290 — 5y
Ad

Answer this question