Hello i was wondering how you could check if a player is some studs away from a model as i want to make it so that a certain script disables after the player walks a certain amount of studs away from that model?
You’d want to use magnitude:
(player.HumanoidRootPart.Position - model.PrimaryPart.Position).magnitude
That’ll give you a number back, if it’s under some amount you could say they’re close to it; otherwise they’re not. Then I’d have that in some kind of check - either a loop, an event connection, etc., but some way that you can check
You can use magnitude to see the distance between 2 things.
To use the magnitude, you must first have access to part of the player, and access to a part in the model. The formula to get the magnitude looks like this: local mag = (torso.Position - modelPart.Position).magnitude
.
Hope this helps!
As stated by the others, you could use the magnitude to determine the distance between two objects. If you prefer to use it multiple times, a function would help.
local function GetDistance(p1,p2)
local Distance = (p1.Position - p2.Position).magnitude
return Distance
end
The function can be run from any instance with a recorded position.
Well once i have a specified distance away how do i use that in a script, i am sorry but what i am getting here is confusing as i am not the best scripter and i have never worked with this.
@FIREYAUTO’s GetDistance
function returns the distance between two objects. Pass in your model’s PrimaryPart as the first parameter, and the player’s HumanoidRootPart as the second parameter and you get the distance between the player and your model. You should put the code inside a LocalScript and use a while loop to continuously check the distance between the player and your model.
First, you should place an invisible part in the center of your model and set it as the PrimaryPart of your model (or if you already have a part in the center of the model, use it). Create a LocalScript inside game.StarterPlayer.StarterCharacterScripts
and paste the following code in:
local humanoidRootPart = game.Players.LocalPlayer.HumanoidRootPart
local modelPrimaryPart = yourModel.PrimaryPart
local function GetDistance(p1,p2)
local Distance = (p1.Position - p2.Position).magnitude
return Distance
end
while true do
-- if player is 10 studs away from model
if GetDistance(modelPrimaryPart, humanoidRootPart) > 10 then
-- stop your script here
end
end
local player = game.Players.LocalPlayer
local MAX_RANGE = 15 --change here
while player:DistanceFromCharacter(workspace.YourModel.Head.Position) < MAX_RANGE do
print("ScriptEnabled = false")
ChoseYourScript.Disabled = true
else
print("ScriptEnabled = true")
ChoseYourScript.Disabled = false
end
Do you know how someone would go about this in a server script?
havent tried this. This should work.
local rs = game:GetService("RunService")
rs.RenderStepped:Connect(function()
for i,v in pairs(workspace:GetDescendants()) do
if v:FindFirstChildOfClass("Humanoid") then
local human = v:FindFirstChildOfClass("Humanoid")
local hrp = human.Parent:FindFirstChild("HumanoidRootPart")
if hrp then
if (hrp.Position - script.Parent.Position).Magnitude < 5 then
print("close")
end
end
end
end
end)
Man, run service don’t work on server scripts.
it does… just use heartbeat instead of renderstepped
your addition was greatly needed to a year old post
Since the topic has already been bumped I might as well respond: Run service does work on server scripts, just not renderstepped
oh, i didn’t know that lol brah limit word
thx, i just literally revived the post man lol