Hello, my script doesn’t work in the way i want it to work. The way I want it to work is when you get next to a object, screen gui and a music plays and when far away the gui and music stops. However I have multiple objects, in my script it checks both of their distance but never checks the closest object.
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 not target then return end
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
local function getNearestBot()
local closestMag = math.huge
local closestIns = nil
for _,bot in pairs(workspace:GetChildren()) do
-- Here is where you can add any checks to make sure the Robot is actually a Robot! Ex):
-- if bot.Name ~= "Robot" then
-- continue
-- end
-- Continue will basically end that line and move to the next iteration of the table
local mag = (bot:GetPivot() - player.Character.HumanoidRootPart.Position).Magnitude
if mag < closestMag then
mag = closestMag
closestIns = bot
end
end
return closestIns,closestMag
end
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 getNearestBot()
local closestMag = math.huge
local closestIns = nil
for _,bot in pairs(workspace:GetChildren()) do
-- Here is where you can add any checks to make sure the Robot is actually a Robot! Ex):
if bot.Name ~= "saulcone" or bot.Name ~= "selene" then
continue
end
-- Continue will basically end that line and move to the next iteration of the table
local mag = (bot:GetPivot() - player.Character.HumanoidRootPart.Position).Magnitude
if mag < closestMag then
mag = closestMag
closestIns = bot
end
end
return closestIns,closestMag
end
while true do
task.wait(0.2)
getNearestBot()
end
Where to put when player is close to nearest bot
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()
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 getNearestBot()
local closestMag = math.huge
local closestIns = nil
for _,bot in pairs(workspace:GetChildren()) do
-- Here is where you can add any checks to make sure the Robot is actually a Robot! Ex):
if bot.Name ~= "saulcone" or bot.Name ~= "selene" then
continue
end
-- Continue will basically end that line and move to the next iteration of the table
local mag = (bot:GetPivot() - player.Character.HumanoidRootPart.Position).Magnitude
if mag >= distance then -- Makes sure that the instance is within distance
return
end
if mag < closestMag then
mag = closestMag
closestIns = bot
end
end
return closestIns,closestMag
end
while task.wait(0.2) do
if getNearestBot() then
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()
else
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