Advanced Roblox Scripting Tutorial #22 - Magnitude (Distance) [Beginner to Pro 2019]
Вставка
- Опубліковано 7 лют 2025
- Advanced Roblox Scripting Tutorial #22 - Magnitude (Distance) [Beginner to Pro 2019]
Hey guys! Welcome back to another advanced roblox scripting tutorial. In today's video I will be explaining magnitude in roblox stuido. Basically how to find the distance between two parts in roblox. It's how you measure distance. The easiest way at least. I used to try to subtract the Vector3 positions and stuff until I found magnitude lol. Thankfully I'm not that bad of a scripter anymore. Anyways super cool stuff coming. I also teach you how to make a Zombie AI that follows you. It's pretty cool :)
-Join my Discord Server to learn and meet other developers!-
/ discord
-- Follow me on twitter! (my name is tapwater because thats my main channel): / realtapwater
-- Follow me on Instagram! (my name is tapwater because of my main channel): / realtapwater
Link to Beginner's Tutorial Series:
• Beginner's Roblox Scri...
--What is this series about and who is it for?
So this series is meant for people who really want to learn exactly how to script, if that is you then I promise by the time you watch this whole series you will know exactly how to script on roblox. Anyways be sure to subscribe to see whenever I release a new tutorial and I will see you guys in the next one. Peace!
HEYy guys! Be sure to join the discord server to stay connected, meet other devs, and learn!: discord.gg/FKcSyRh
ur first comment lol
Why would it need to be subtracted when calculating the magnitude?
It says "The current identity (2) cannot Class security check (lacking permission 6)" what should I do? My Code:
local ztorso = script.Parent.Torso
local zhumanoid = script.Parent.Humanoid
function walker()
local agro = 100
local target = nil
for i,v in pairs(game:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v ~= script.Parent then
if (ztorso.Position - torso.Position).magnitude < agro then
agro = (ztorso.Position - torso.Position).magnitude
print("found em")
target = torso
end
end
end
return target
end
while true do
wait(3)
local s = walker()
if s then
zhumanoid:MoveTo(s.Position)
else
zhumanoid:MoveTo(Vector3.new(math.random(-10,10),0,math.random(-10,10)))
end
end
@@rosultra4067 do
while wait(1) do
@@ali.2412 i think we can use pathfinding service (i think that's will work)
this was honestly the funniest, and fun-est video in the entire series so far. I like when he puts jokes, it really brightens the mood and makes it more fun to learn.
Fr
You're the champion Tap.
The reason why your the champ is you can easily upload on your big channel and get thousands of views but you choose to upload here.
I really appreciate it man i'm understanding more and more and i really enjoy scripting and cant wait for more videos to come!
So glad I could help :)
My goal is to teach everyone that wants to learn scripting how to script. Even if you have no experience!
fart
@@duckking404 i agree
@@duckking404 i also agree
@@duckking404 very true
Kudos to you for being able to teach coding greatly . It's so hard to use your voice loudly without stuttering wanting to be able to process the information you can.
"They got an upper torso,"
**clicks lower torso**
"And they got a lower torso."
**clicks upper torso**
great tutorial btw
Bro I literally just saw that and decided to scroll down in the comments, Lol 🤣
Anyone who is experiencing problems, but aren't getting any errors in the output:
Check that the *HumanoidRootPart* of your NPC is Anchored. If it is Anchored then your NPC can't move, even after being instructed to. This confounded me for a few days. I know I can't be the only person who encountered this, so please like this comment for exposure.
Thanks, I thought it was outdated.
@@Concealed461 same
I love you thanks
Ty you are the best
bro thank you
i like how you used my name as the bot
lol
it's rick astley
@@Cadmium141 are you actually dream
@@akanoxy391 No, it’s quite obvious that it isn’t him.
@@RealCalamity Great. You’ve made my wife pregnant.
I WILL NEVER GONNA GIVE UP ABOUT SCRIPTING
I'm also never gonna let my self down from scripting
I'm never gonna run around and desert scripting
Never gonna make scripting cry
Never gonna say Math.Pi()
@@captainkittty3780 lmao
The advanced scripting playlist is over 10 hours long.
If you've made it here you should be proud of yourself!
And also thanks to TheDevKing for spending probably well over 10 or 20 hours making these tutorials for us!
ive officially finished them, though some I skim watched them (such, as animations, sounds and welds)
@@RichRBLX me too
i really like the way you talk to the NPC
this guy so jolly it makes me smile
holy crap dude. im literally 4 minutes in this lesson and i already got my problem solved. thank you!
omg first it did work and i started crying, then it was just because I need to add a ")' at the end of the vector 3 randomizer. Nice vid
for people who don't understand why we're lowing down the magnitude, It's because we want the Npc to focus on the one target because if multiple, it would start doing something weird
I didnt knew, how useful it is, before 1:10
The fact that Rick is actually Rick Astley
4:38 TheDevKing: "so that's why they're considered 12 studs away"
Captions: "so that's why they're considered 12 cells away"
ha
When your printing it and you just want a number without a decimal use
print(math.floor(distance))
print(math.floor(distance) or math.ceil(distance))
print(math.round(distance))
i am filling a whole notebook with information from your videos
2 years later but same lol
The entire viewerbase just got rickrolled
nice tutorials :)
Thanks :)
Also cool that you made a zombie. That helps a LOT. I can litterally now make my own Enemy
When the positions are subtracted the difference should have sign (negative or positive). The "Magnitude" method should be the one that gets rid of the sign, and therefore the order of subtraction does not matter.
tap we need the zombie ai series.. please! I know you probably won't see my comment because you're busy and stuff but you said it in your video right here: 5:55, so please can you make a series?
TheDevKing is probably the best Roblox scripting teacher on the entire UA-cam platform. He explains everything so well and if you don't understand something he says you will probably figure it out by the end of the video. Great videos! keep it up.👍
nah the devking is nothing compared to alvinblox
Great job , great simple explanation with every line. Superb. Thank you
1:30 im not gonna do that zombie script.
subtitles:Zambia strips
when will he realize that he has been rickrolling himself this entire time..
you could have made lots of money teaching these lessons but instead you upload them for anyone to learn for free, thank you
I was the 1,000 like
Dude u helped me so much I can learn how to make a zombieai
Cool another part!
best roblox tutorial ever!
This is my first time getting rick rolled in roblox, hope it's my last
hey i like ur adult vids made me big
@@actorman3895 wat?? I don't understand a single thing you said you ding dong
@@actorman3895 you've watched too much pornhub you dumbfuck
Hi
@@actorman3895 SUS
Use FindFirstChildOfClass("Humanoid") Instead of FindFirstChild("Humanoid") so it can attack all characters that have humanoid not characters that have humanoid named "Humanoid"
findfirstchild finds the first item named humanoid, findfirstchildofclass finds the first child that is a specific class
@@per_cept Yeah i know.
@@per_cept REALLY??? i wouldnt have guessed
@@Xelarix k
@@MarioMario-yb3iz "k" what?
tysm ur the best I thank u like crazy thank u so much ur the best utuber
Thank you now I can make detection for my stealth/loud game
Thanks! This Helped Alot!
Imagine not being subscribed, can't relate
Great tutorial
when he counted to nine I knew what he meant but i laughed hysterically
A good way to learn everything about scripting is watch all of his playlist videos so you know everything there is to know to make a good game, and list the tutorials that you think he didnt make sense in, then look up other tutorials for those thing's because other people may explain in a different way that might make more sense. And also dont sit there and be stuck, if it dont work after trying multipule times its prolly outdated, these videos kinda old
So instead of using a sphere hitbox, I used magnitude instead, cuz I hate lag
RIP Rick
R.I.P. He will be missed!
F to pay respects to me
@@TheDevKing Rick rolled
ua-cam.com/video/dQw4w9WgXcQ/v-deo.html
omg very cool tutorial
idk know why but your voice kinda sounds like a deeper versiom of Mclovin from SuperBad
Minor improvement to the zombie tracking
while wait() do
local torso=findTarget()
if torso then
zombHumanoid:MoveTo(torso.Position)
else
wait(1)
zombHumanoid:MoveTo(zombTorso.Position+Vector3.new(math.random(-50,50),0,math.random(-50,50)))
end
end
by making it wait() and adding wait(1), the zombie will redirect itself much more frequently and still roam around normally. This probably won't matter cause this zombie script is too simple for any big game but it's fun to mess around 🗿
bro using wait() isnt usefull at that point just use runservice gosh darn u just ruined the point of it heres an actual improvement
local rs = game:GetService("RunService")
local function update()
local torso=findTarget()
if torso then
zombHumanoid:MoveTo(torso.Position)
else
wait(1)
zombHumanoid:MoveTo(zombTorso.Position+Vector3.new(math.random(-50,50),0,math.random(-50,50)))
end
end
rs.Heartbeat:Connect(update)
Agro, slang/ short for Aggravated :)
No it’s aggressive
@@traviskindalit4346 It can mean both, good point tho lol
code:
local zombieTorso = script.Parent.Torso
local zombieHumanoid = script.Parent.Humanoid
local function findTargret()
local agroDis = 1000
local Targret = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v ~= script.Parent then
if (zombieTorso.Position - torso.Position).magnitude < agroDis then
agroDis = (zombieTorso.Position - torso.Position).magnitude
Targret = torso
end
end
end
return Targret
end
while wait(1) do
local torso = findTargret()
if torso then
zombieHumanoid:MoveTo(torso.Position)
end
end
tysm bro, the only problem i had was that i put the script in the workspace instead of the actual Zombie xd silly me.
i wouldnt of found out ;D
does anyone know a script if there is a lot of spawns in a game
but we want to make 1 spawn as the start spawn where all the players that join spawns there
at the start spawn?
please im making a game ,
its kind of a escape obby game
but everytime a second player join they spawn in the second stage im so confused why that happend
i want it to start at the start point
please someone:(
i was able to make it so when the zombie gets close to the npc the npc starts running around
just did it too and published it as a game
Cool!
very good tutorial but can i ask:
how to make the zombie search humanoid again after he found and killed one?
what if the "zombie" is a bullet, how do you make the bullet go to the zombie???
This is so helpfull
If you are making a killer with magnitude make sure to use pathfinding service
If there were multiple characters around, this would still work right? It would find the nearest character to track instead of breaking all together, because for some reason that's what mine is doing..
i think you have to create a new function of "finding the nearest character" whenever the previous function is complete.
so, how did they kill rick? There wasn't any touched event or anything-
It’s gonna kill him from a distance
23:20 rick astley has been killed. Person who liked to rick roll.
If your NPC is having a stroke when you're trying to make him go to random positions then do this:
zombieHuman:MoveTo(zombieTorso.Position + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50)))
zombieHuman.MoveToFinished:wait()
It'll walk to the position and wait the lowest value
don't need it but that's *very based*
why did i get "Argument 1 missing or nil" on the for in loops line idk what it means pls help
19:47 -- am I the only one who notices that
I was asking for vid on how to make a player warp a distance in where it's facing and now I feel stupid
Time to try a moving noc
there's this stage i am building for an obby game, in which players must move unanchored object onto a green plate which unlocks doors to pass thru. now the real question is do i have to use Magnitude and min/max distance variables like script shown in the video for this stage to work? this question is for everybody reading the comment section, would appreciate help.
No
i know that u probably finished the game at this point, but if u didnt
u can just use Touch Functions, alot more simple and easy
Could I do that zombie script with a normal part? Do I have to put a humanoid in it?
i am going to make a useful module with this
0:29 You Couldn't get this from any other guy
I just got RickRolled by trying to learn script...
You almost dropped the F-Bomb!
Not shaming or hating you for it.
how can you call the HumanoidRootPart from a character so i can move it to where if you clock something it can teleport you????
What was the noise I wonder :thinking:
I think I found it out, it was a chair squeaking noise I believe
implemented path finding:
local pathfinding = game:GetService("PathfindingService")
local allworkspace = game.Workspace:GetChildren()
local btorso = script.Parent.Torso
local bhuman = script.Parent.Humanoid
local function findtarget()
local argodistance = 50
local target
for i, v in pairs(allworkspace) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v ~= script.Parent then
--check distance
if (btorso.Position - torso.Position).magnitude < argodistance then
argodistance = (btorso.Position - torso.Position).magnitude
target = torso
end
end
end
return target
end
local torso = findtarget()
local path = pathfinding:CreatePath()
path:ComputeAsync(btorso.Position, torso.Position)
local waypoints = path:GetWaypoints()
while wait(1) do
if torso then
local path = pathfinding:CreatePath()
path:ComputeAsync(btorso.Position, torso.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
local bal = Instance.new("Part")
bal.Shape = "Ball"
bal.Material = Enum.Material.Neon
bal.Position = waypoint.Position + Vector3.new(0,2,0)
bal.Size = Vector3.new(0.6, 0.6, 0.6)
bal.Anchored = true
bal.CanCollide = false
bal.Parent = game.Workspace
bhuman:MoveTo(waypoint.Position)
bhuman.MoveToFinished:Wait(2)
end
else
print("no torso")
bhuman:MoveTo(btorso.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50)))
wait(3)
end
end
9:00 “Cause theres 6 partyparts”
Lmao
Oh yes magnitude this will work to measure how long will my car go
For me, it finds the target but it doesnt move to it.Why?
I can't compare the Vector3 Value to the number value
*random squeak* "oops sorry that was a bad noise"
in 2022 the movements are super weird but tutorial was good
--targets now take damage, pretty simple :)
local zombieTorso = script.Parent.HumanoidRootPart
local zombieHumanoid = script.Parent.Humanoid
local function findTarget()
local agroDistance = 100
local hitDistance = 6
local target = nil
for _, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("HumanoidRootPart")
local npc = v:FindFirstChild("ZombieNPC")
if human and torso and v ~= npc then
if (zombieTorso.Position - torso.Position).magnitude < agroDistance then
agroDistance = (zombieTorso.Position - torso.Position).magnitude
target = torso
if (zombieTorso.Position - torso.Position).magnitude
but the health depletes even when the npc does not touch u
It does not work for me because roblox studio thinks position is a model.
1:01 no more rick rolls 😭😭😭😭😭
How do i make my model deal damage (I didnt use the model in the video)
When you realise the TDK rickrolled you TWICE: 💀
Wait I'm confused. Why did you had to do a while loop while you could just do local torso = findTarget()
if torso then
zombieHumanoid:MoveTo(torso
.Position)
Or wait(1) local torso = findTarget()
if torso then
ZombieHumanoid:MoveTo(torso.Position)
If you didn't figure it out yourself yet it's so that every second the zombie will recheck its distance from the target and either redirect itself or stop chasing. Without the loop it would either walk straight to the nearest target without ever turning or just not detect anything in range and never move. It also needs it to wander around randomly. Hope that helps.
Can I do this but with a delorean?
0:35
Cool my name is Rick too!
Rick Mcnikkins
In line 10, roblox dosent accept -= as "not equal to" and I dont know what I should use as replacement
~=
make sure you are using the tilda ~ instead of minus sign like ~= instead of -=
17:07 saving my time XD
when you realize you've been ricked rolled
Love it
I feel sorry for Rick :(
How would I make a script where people who're in the radius magnitude of 200 is damaged?
i dont think thats anything to do with magnitude im pretty sure you check if the player is touching an invisible part or either set up a region3
how do you do in a certain range they die instantly
DEVTHEKING❤️🔥
bro really rickrolled us
The findtarget function didn't work for me so I used the function FindPeople()
Corrected version i think: local PathfindingService = game:GetService("PathfindingService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()
path:ComputeAsync(humanoidRootPart.Position, game.Workspace.Part.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
humanoid:MoveTo(game.Workspace.Part.Position)
What's the sense of Pathfinding if we have this?
pretty sure if u hide behind a wall while the zombie is chasing you it would just walk into the wall, because you're only using the "humanoid:moveto" function. You could add custom waypoints but pathfinding is just better.
magnitude is used for hitboxes for some games
How do you make a stud counter using magnitude???
It did not work for me at all :(