Any problems with this distance checking?

All right, so in my last post, i asked how i would prevent the flinging. I made it but my friend still somehow found a way around it.
So i used magnitude, but now i cant dash anywhere, i gotta make the map so large if i wanna use dashing, so simple solution, check every second if the player gets near the map.
As you can guess, this script doesn’t work
Any answers why?

remote.Event:Connect(function(character,plr,vel)
	RunService.Heartbeat:Connect(function(step)
		local distance = 4
		local check = false
		for _, a in pairs(Maps) do
			for _, v in pairs(a:GetChildren()) do
				if v:FindFirstChild("HumanoidRootPart") or v:FindFirstChild("PrimaryPart") then
					local HumRoot = v:WaitForChild("HumanoidRootPart") or v:WaitForChild("PrimaryPart")
					local plrRoot = character:WaitForChild("HumanoidRootPart")
					if (HumRoot.Position - plrRoot.Position).magnitude <= distance then
						print("removing..")
						Debris:AddItem(vel,.1)
					else
						return
					end
				end
			end
		end
	end)
end)

Edit: if i make prints before:

if (HumRoot.Position - plrRoot.Position).magnitude <= distance then

It prints.

What’s the error? Just so I can see what I’m actually dealing with.

It doesn’t actually print true (when i print)

When im really close to the dummies.

Wait so you’re trying to make a Dashing Script I’m guessing? If so, I’ll make you a quick one because the method you’re using wouldn’t really work or is very efficient in-terms of performance.

I have already made one, but the problem is that when i run into a part that is can collide true, it flings me.

Are you tweening or using BodyPosition for your Dash?

Im using body velocity, maybe tweening or body pos is better and can maybe solve this?

Body Position will solve it 100%, tweening wouldn’t be that efficient since you would run slightly into the same problem but BodyPos is a lot more efficient in creating a DashingController.

local game = game

-- // Constants \\ --
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local UserInputService = game:GetService("UserInputService")
local Debris = game:GetService("Debris")

-- [ LocalPlayer ] --
local LocalPlayer = Players.LocalPlayer

local Character = LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart", 5)
local Humanoid = Character:WaitForChild("Humanoid", 5)
local Animator = Humanoid:WaitForChild("Animator", 5)

-- // Variables \\ --
local KeyCode = Enum.KeyCode

local LastDash = time()
local Keys = {
	[KeyCode.W] = time();
	[KeyCode.A] = time();
	[KeyCode.S] = time();
	[KeyCode.D] = time();
}

local LockedTarget = ReplicatedStorage.LockedTarget

-- [ Animations ] --
local Animations = {
	[KeyCode.W] = script:WaitForChild("FrontDash", 2.5);
	[KeyCode.A] = script:WaitForChild("LeftDash", 2.5);
	[KeyCode.S] = script:WaitForChild("RightDash", 2.5);
	[KeyCode.D] = script:WaitForChild("BackDash", 2.5);
}
for i,v in pairs(Animations) do
	Animations[i] = Animator:LoadAnimation(v)
end

-- [ Micro Optimization ] --
local MaxForce = Vector3.new(1, 0, 1) * 1e5
local NewInstance = Instance.new

-- // Event Listeners \\ --
UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end
	local InputKey = Input.KeyCode
	local KeyTime = Keys[InputKey]
	if KeyTime then
		local CurrentTime = time()
		if CurrentTime - KeyTime < 0.25 and CurrentTime - LastDash > 0.4 then
			local BodyPosition = NewInstance("BodyPosition")
			BodyPosition.P = 3e3
			BodyPosition.D = 1e3
			BodyPosition.MaxForce = MaxForce
			BodyPosition.Parent = HumanoidRootPart
			Debris:AddItem(BodyPosition, 0.25)

			local RootCFrame = HumanoidRootPart.CFrame
			if InputKey == KeyCode.W then
				BodyPosition.Position = RootCFrame.Position + (RootCFrame.LookVector.Unit * 75)
			elseif InputKey == KeyCode.A then
				BodyPosition.Position =  RootCFrame.Position + (-RootCFrame.RightVector.Unit * 75)
			elseif InputKey == KeyCode.S then
				BodyPosition.Position = RootCFrame.Position + (-RootCFrame.LookVector.Unit * 75)
			elseif InputKey == KeyCode.D then
				BodyPosition.Position = RootCFrame.Position + (RootCFrame.RightVector.Unit * 75)
			end
			Animations[InputKey]:Play()
			LastDash = time()
		end
		Keys[InputKey] = time()
	end
end)

This is what I do which makes it 100% more efficient.

Thanks so much!! Ill definitely try that! :smiley:

Dont worry ab the local game = game LOL

One more question, is it better being handled in the server side? I guess so, makes it less vulnerable..?

Well making it on the ServerSide would be laggy depending on how many players you’re going to have in the Server.

Doing it local sided means they can spam it though, correct me if im wrong.

You can add a waitTime to how many times a Player can spam it, no real exploiter can change that unless you add it as a IntValue under the script and base the time off that Value, make sure it’s in the script as a variable, not only that but the only thing the exploiter can change is the BodyPosition properties. It’s either really choosing vulnerability or lag, sadly roblox has it’s limits.