How would i detect the distance between the player and a part? Also, how would i check if the part has certain named part in it?
1 Like
local part = game.Workspace["YourPart"]
while true do
for i, plrs in pairs(game:GetPlayers()) do
local hrp = plrs.Character.HumanoidRootPart
local distance = (hrp.Position - part.Position).Magnitude
if distance <= 5 then -- the number here is basicly the range
print("Found a player in 5 or less studs close to part")
-- do your code idk
end
end
task.wait(0.5)
end
1 Like
And to check if a part has a certain part named a specific thing
local part = game.Workspace.part
for i, v in pairs(part:GetDescendants()) do
if v.Name == "hamburguer" then
print("in" .. part.Name .. "was found a hamburguer"
end
end
1 Like
The is your code with some extra things, but i dosent work.
local v20 = v1:GetDescendants()
while true do
for i,v in pairs(v20) do
if v.Name == "NPC" then
for i, v21 in pairs(v1:GetPlayers()) do
local v22 = v21.Character.HumanoidRootPart
local v23 = (v22.Position - v.Position).Magnitude
if v23 <= 5 then -- the number here is basicly the range
print("Found a player in 5 or less studs close to part")
-- do your code idk
end
end
end
end
task.wait(0.5)
end
Is there a way to make it work?
I also change the variables to vNumber, because its more organized (for me anyways)
v1 is workspace
1 Like
for i, v21 in ipairs(v1:GetPlayers()) do
You can’t :GetPlayers()
from workspace, you can only do that via Players service.
while true do
for i, part in pairs(workspace:GetDescendants()) do
if part.Name == "NPC" then
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
if player:DistanceFromCharacter(part.Position) <= 5 then
print("Found a player in 5 or less studs close to part")
end
end
end
end
task.wait(0.5)
end