Hello, I am currently coding a game where you run away from killbricks that pathfind to you. This game is working out pretty well, except for the fact that the bricks lag behind you. When you run away from the killbrick at a constant speed, it never really catches up to you. It’s hard to explain, but here’s an example of what I want to achieve:
I’ve tried everything I could, from tutorials to other devforum posts, you can even see that this is my first devforum post, because I couldn’t find any solutions!
Here’s my code (I know, I know. It’s very unoptimized. Please don’t reply with any optimization tips if you’re not trying to help me with my main issue, but I’ll accept them.)
local ps = game:GetService("PathfindingService") -- get this service
local ts = game:GetService("TweenService")
local info = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)
repeat task.wait() until game:GetService("ContentProvider").RequestQueueSize == 0 -- wait until the game fully loads
local closestDistance = math.huge
local closestPlayer = nil
local target
local newPos = nil
--local brickSignal = game.Workspace:WaitForChild("brickSignal")
task.wait(3)
local brickSpeed = 0.1
while task.wait() do
-- run everything below forever
--if brickSignal.Value == 1 then
--task.wait(0.1) -- if you don't want to put too much pressure on the server, add a small cooldown
closestDistance = math.huge -- reset the closest distance so that we can find it over and over
closestPlayer = nil
for i,v in pairs(game.Players:GetPlayers()) do -- get every player in the game
if v and v.Character and v.Character:FindFirstChild("HumanoidRootPart") then
local distance = (v.Character:FindFirstChild("HumanoidRootPart").Position - script.Parent.Position).Magnitude -- get every player's distances
--print(distance) -- debug
if distance <= closestDistance and distance then -- if the distance of the player is smaller than the previous closest distance, then run the code below
closestDistance = distance
closestPlayer = v.Character:FindFirstChild("HumanoidRootPart")
--print(closestDistance.. " is the closest distance")
script.Parent.Orientation = closestPlayer.Orientation
end
end
end
if closestPlayer then
target = closestPlayer.Position
else
target = script.Parent.Position
script.Parent.Position = script.Parent.Position
end
-- the target to pathfind to
local path = ps:CreatePath() -- create a path
local pathfind = path:ComputeAsync(script.Parent.Position, target) -- create a path between this npc's humanoid root part and the closest player's humanoid root part
for z,waypoint in pairs(path:GetWaypoints()) do -- get all of the waypoints
local newerPos = waypoint.Position
local bestTween = ts:Create(script.Parent, info, {Position = newerPos})
bestTween:Play()
task.wait(math.clamp(brickSpeed, 0.05, 2)/closestDistance)
end
if path.Status == Enum.PathStatus.Success then
--print("i'm coming for you")
script.Parent["Audio/stone_drag"].Playing = false
else
if closestPlayer then
newPos = closestPlayer.Position
else
newPos = script.Parent.Position
end
local bestTween = ts:Create(script.Parent, TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false), {Position = newPos})
--game.Chat:Chat(script.Parent, "better run", Enum.ChatColor.Red)
script.Parent["Audio/stone_drag"].Playing = true
bestTween:Play()
end
path.Blocked:Connect(function(b)
warn("path blocked")
end)
--end
end
Here’s a visual on the issue:
As you can see, even though the brick is supposed to be extremely fast, it starts lagging behind you. I really have tried everything.
Thank you all and I look forward to replies.