Hey, I have 2 specific objects and I need some help with finding the closet object to a player.
Then it would run a segment, I needed to do this because I realised the for i loop checks those 2 objects without finding the closest one.
My script:
local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local distance = 20
local tweenservice = game:GetService("TweenService")
local info = TweenInfo.new(
0.3,
Enum.EasingStyle.Quad,
Enum.EasingDirection.InOut,
0,
false,
0
)
local function nearnextbot()
for i, v in pairs(workspace:GetChildren()) do
local target = v
--local poop = player.Team == Teams["noobs"]
--local poop1 = player.Team
if target and target.Name == "saulcone" or target.Name == "selene" then
local target2 = v.HumanoidRootPart
if (target2.Position - hrp.Position).Magnitude <= distance then
print("play")
if script.Parent["Running Chase b"].Playing == false then
script.Parent["Running Chase b"]:Play()
end
tweenservice:Create(script.Parent["Running Chase b"], info, {Volume = 1}):Play()
tweenservice:Create(script.Parent.ImageLabel, info, {ImageTransparency = 0}):Play()
elseif (target2.Position - hrp.Position).Magnitude >= distance then
tweenservice:Create(script.Parent.ImageLabel, info, {ImageTransparency = 1}):Play()
print("end")
tweenservice:Create(script.Parent["Running Chase b"], info, {Volume = 0}):Play()
task.wait(0.3)
script.Parent["Running Chase b"]:Stop()
end
end
end
end
while true do
task.wait(0.2)
nearnextbot()
end
This script has a problem that needs to be replaced which is
if target and target.Name == “saulcone” or target.Name == “selene” then
The script is in LocalScript