I learned that Parallel Lua would increase performance but I am not really seeing a difference when using it.
local RunService = game:GetService("RunService")
local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local Heroes = workspace:WaitForChild("Heroes")
local target = nil
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local function GetNearestTarget()
local nearestDistance = math.huge
for i,v in Heroes:GetChildren() do
if v:IsA("Model") and v.PrimaryPart then
local humanoid = v:FindFirstChildOfClass("Humanoid")
local distance = (HumanoidRootPart.Position - v.PrimaryPart.Position).Magnitude
if humanoid and humanoid.Health > 0 and distance < nearestDistance then
nearestDistance = distance
target = v
end
end
end
end
HumanoidRootPart.Touched:Connect(function(hit)
if not hit:IsDescendantOf(character) then
local speed = HumanoidRootPart.AssemblyLinearVelocity.Magnitude
if speed >= 1 then
humanoid.Jump = true
end
end
end)
RunService.Heartbeat:Connect(function()
task.desynchronize()
GetNearestTarget()
if target and target.PrimaryPart then
task.synchronize()
humanoid:MoveTo(target.PrimaryPart.Position)
task.desynchronize()
local rayOrigin = HumanoidRootPart.Position
local rayDirection = HumanoidRootPart.CFrame.LookVector * 2
local raycast = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycast then
task.synchronize()
humanoid.Jump = true
task.desynchronize()
end
end
end)