Hello, I am making my chase script for an entity but how do I detect players if they’re out of range.
Script:
local LatestRoom = game.ReplicatedStorage.LatestRoom
local Entity = script.Parent
local Speed = 5
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
Entity.Ambience:Play()
function findNearestTorso(pos)
local list = Players:GetPlayers()
local torso = nil
local dist = 50
local temp = nil
for _,player in pairs(list) do
local char = player.Character
local rootPart = char and char:FindFirstChild("HumanoidRootPart")
if not rootPart then continue end
local distance = (rootPart.Position - pos).Magnitude
if distance >= dist then continue end
torso = rootPart
dist = distance
if distance <= dist then
player.PlayerGui.A380Run.ImageLabel.ImageTransparency = 0
end
end
return torso
end
task.wait(3)
Loop = RunService.Heartbeat:Connect(function()
for i, plr in ipairs(Players:GetPlayers()) do
local EndPos = findNearestTorso(Entity.Position)
local Distance = (Entity.Position - EndPos.Position).Magnitude
local Calc_Speed = Distance / Speed
local chase = TweenService:Create(Entity, TweenInfo.new(Calc_Speed), {CFrame = EndPos.CFrame})
chase:Play()
end
task.wait(100)
Loop:Disconnect()
for i, plr in ipairs(Players:GetPlayers()) do
plr.PlayerGui.A380Run.ImageLabel.ImageTransparency = 1
end
Entity:Destroy()
end)
Just see if the distance between the entity and the player is more than a certain value?
local distance = (Entity.HumanoidRootPart.Position-PlayerToFollow.HumanoidRootPart.Position).Magnitude
if distance > 200 then
-- Stop following player.
end
local function FindVictim()
local maxdistance = 50
for i, plr in game.Players:GetPlayers() do
if plr and plr.Character then
local distance = (rootPart.Position - pos).Magnitude
if distance <= 50 then
plr.PlayerGui.A380Run.ImageLabel.ImageTransparency = 1
else
print("Out of range")
end
end
while true do
FindVictim()
task.wait()
end
Then just set it to 1? I gave you an example but I’ll help you on that:
local LatestRoom = game.ReplicatedStorage.LatestRoom
local Entity = script.Parent
local Speed = 5
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
Entity.Ambience:Play()
function findNearestTorso(pos)
local list = Players:GetPlayers()
local player1 = nil
local dist = 50
local temp = nil
for _,player in pairs(list) do
local char = player.Character
local rootPart = char and char:FindFirstChild("HumanoidRootPart")
if not rootPart then continue end
local distance = (rootPart.Position - pos).Magnitude
if distance >= dist then continue end
player1 = player
dist = distance
if distance <= dist then
player.PlayerGui.A380Run.ImageLabel.ImageTransparency = 0
end
end
return player1
end
task.wait(3)
Loop = RunService.Heartbeat:Connect(function()
for i, plr in ipairs(Players:GetPlayers()) do
local player = findNearestTorso(Entity.Position)
local EndPos = player.Character.HumanoidRootPart
local Distance = (Entity.Position - EndPos.Position).Magnitude
local Calc_Speed = Distance / Speed
local chase = TweenService:Create(Entity, TweenInfo.new(Calc_Speed), {CFrame = EndPos.CFrame})
chase:Play()
if Distance > 100 then -- There is no `player` element defined here, so you need to define it like I did.
player.PlayerGui.A380Run.ImageLabel.ImageTransparency = 1
end
end
task.wait(100)
Loop:Disconnect()
for i, plr in ipairs(Players:GetPlayers()) do
plr.PlayerGui.A380Run.ImageLabel.ImageTransparency = 1
end
Entity:Destroy()
end)
local LatestRoom = game.ReplicatedStorage.LatestRoom
local Entity = script.Parent
local Speed = 5
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
Entity.Ambience:Play()
function findNearestTorso(pos)
local list = Players:GetPlayers()
local torso = nil
local dist = 50
local temp = nil
for _,player in pairs(list) do
local char = player.Character
local rootPart = char and char:FindFirstChild("HumanoidRootPart")
if not rootPart then continue end
local distance = (rootPart.Position - pos).Magnitude
if distance >= dist then continue end
torso = rootPart
dist = distance
player.PlayerGui.A380Run.ImageLabel.Visible = distance <= dist -- Rather than using transparency, use this.
end
return torso
end
task.wait(3)
Loop = RunService.Heartbeat:Connect(function()
for i, plr in ipairs(Players:GetPlayers()) do
local EndPos = findNearestTorso(Entity.Position)
local Distance = (Entity.Position - EndPos.Position).Magnitude
local Calc_Speed = Distance / Speed
local chase = TweenService:Create(Entity, TweenInfo.new(Calc_Speed), {CFrame = EndPos.CFrame})
chase:Play()
end
task.wait(100)
Loop:Disconnect()
for i, plr in ipairs(Players:GetPlayers()) do
plr.PlayerGui.A380Run.ImageLabel.ImageTransparency = 1
end
Entity:Destroy()
end)
You are using RunService.Hearbeat, which lets say create a new thread each time it runs which could be each 0.15 seconds +/-. On each “iteration” of it, you create a new thread that ignores previous thread (if completed or not) and that thread has 100 seconds task.wait(100), meaning, if 2000 threads were running, those will start to fire the “end” (the code after the 100 seconds) 2000 times, running the final code of your RunService connected function…
This:
Loop:Disconnect()
for i, plr in ipairs(Players:GetPlayers()) do
plr.PlayerGui.A380Run.ImageLabel.ImageTransparency = 1
end
Entity:Destroy()
Will run thousand of times when the 100 seconds happens…
The approach is not right, and thats not how a RunService should be used.
Just give it a try, test this code and check the output, you will get the code after the task.wait(10) multiple times, and a print of the delta time each time the RunService did run:
local heartBeat
heartBeat = game:GetService("RunService").Heartbeat:Connect(function(delta)
print(delta) -- the heartbeat
task.wait(10) -- the "wait"
heartBeat:Disconnect() -- stoping the HeartBeat
warn("thread ending code happens") -- the code that will repeat multiple times which makes no sense
end)
Those are not the only issues with OP’s thread FYI/
E.G: OP is using tweenservice in a loop which would break the movement completely. OP is using a disconnect function in the loop itself which would trigger ~ 6,000 times after the 100 seconds end.
A really good way to fix all these issues is to do it like this:
local Loop
task.delay(100,function()
Loop:Disconnect()
for i, plr in ipairs(Players:GetPlayers()) do
plr.PlayerGui.A380Run.ImageLabel.ImageTransparency = 1
end
Entity:Destroy()
end)
Loop = RunService.Heartbeat:Connect(function(deltaTime)
local EndPos = findNearestTorso(Entity.Position)
local Distance = (Entity.Position - EndPos.Position).Magnitude
local Calc_Speed = Distance / Speed
Entity.CFrame = Entity.CFrame:Lerp(EndPos.CFrame,Calc_Speed*DeltaTime)
end)
Something like this, it may have it’s errors because I wrote this in the website but it’s basically how the code could be optimized.
I know, and Im totally agree, I just didnt want to make my topic larger xD.
There are plenty of issues on that approach. Im just giving my little contribution to the topic,