How would I check the distance of the player from the ball?

Hi.

So I am making a ROBLOX “Soccer” game and there is this slide tackle script that is overpowered, and I want to make it so the player can only slide tackle if they are within a close range of the ball.

The current function I am using:

function CloseEnoughToABall()
	--[[
		unfortunately you dont use
		collectionservice to track the balls or anything in general, so i have to use
		more inefficient method.
	--]]
	local Character = Player.Character
	if not Character then return end
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	if not HumanoidRootPart then return end
	local CharacterPosition = HumanoidRootPart.Position

	-- DFC stands for distance from character
	local ClosestBall
	local ClosestBallDFC

	local Children = game:GetService("Workspace"):GetChildren()
	for _, Child in pairs(Children) do
		if Child.Name == "Eve" then
			local DescendantPosition = Child.Position
			if ClosestBall and ClosestBallDFC then
				local DistanceFromCharacter = (CharacterPosition - DescendantPosition).Magnitude
				if DistanceFromCharacter < ClosestBallDFC then
					ClosestBall = Child
					ClosestBallDFC = DistanceFromCharacter
				end
			else
				local DistanceFromCharacter = (CharacterPosition - DescendantPosition).Magnitude
				ClosestBall = Child
				ClosestBallDFC = DistanceFromCharacter
			end
		end
	end

	if ClosestBallDFC then
		if ClosestBallDFC < Range then
			return true
		else
			return false
		end
	else
		return false
	end
end
Mouse.Button1Down:connect(function()
	if not CloseEnoughToABall() then return end
	if Equipped == false then return end
	if TM.GetDebounce() then return end

-- script below this, works so i decided not to paste it in
end)

Any help would be appreciated, thanks!

2 Likes

You already seem to be doing a magnitude check, which is sufficient.
I assume by “script” you mean something that exploiters execute? If so, keep in mind that exploiters can either spoof or completely disable your check in order to bypass it. The only real way to fix this is by handling the magnitude checks on the server.

1 Like

magnitude

1 Like

No, I want the player only to be able to slide tackle if they are near the ball, not from anywhere across the map, not about exploiters bypassing my scripts.

2 Likes

What do you mean, I am already using magnitude checks.
It just is not working for some reason…

1 Like