Closest target not working as intended

I’m trying to make a rake target thing and I’m testing out some stuff. I’m testing it with a wall (Anchored) and it’s extremely buggy. It only works after a bit
Script:

local Part = workspace.TestPart

while wait(0.1) do
	for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
		local CharacterAdded = value.Character or value.CharacterAdded:Wait()
		local HRP = value.Character.HumanoidRootPart
		local RakeModel = Part

		local Distance = (HRP.Position - RakeModel.Position).Magnitude
		if Distance <= 100 then
			warn("hey!")
			if Distance < Part.CurrentTargetMagnitude.Value then
				Part.CurrentTargetMagnitude.Value = Distance	
			end
		end
		print(Distance)
	end
end

What do you mean extremely buggy? Your code seems fine but I don’t really see an issue.

It doesn’t appear to calculate right. My friend is currently doing school related matters and can help me within half a hour. I was closer to the part then he was, however it still counted him as closest.

Use pairs instead of ipairs.

Would there be much of a difference?

Yes, ipairs skips or completely stops for loop when object in table is nil (doesn’t exist) using it, pairs gets all objects in table.

1 Like