How to check if the player is within distance?

I am making a knife script, and I couldn’t figure out how to get the distance between player and hitbox.
I would get this error: Workspace.Knife.Handle.Script:36: attempt to compare number <= Vector3

This is the full script:

local tool = script.Parent.Parent
local handle = tool.Handle
local debounce = false

-- Config --
local dmg = tool.Configuration:GetAttribute("Damage")
local dmgType = tool.Configuration:GetAttribute("DamageType")
local cd = tool.Configuration:GetAttribute("Cooldown")
local hitboxSize = tool.Configuration:GetAttribute("HitboxSize")
local animID = tool.Configuration:GetAttribute("animID")

-- Main --
tool.Activated:Connect(function()
	if debounce then return end
	debounce = true
	-- User Character // Animation --
	local char = tool.Parent
	local humanoid = char:WaitForChild("Humanoid", 10)
	local animator = humanoid:WaitForChild("Animator", 10)

	local swingAnimation = Instance.new("Animation")
	swingAnimation.AnimationId = "rbxassetid://"..animID
	local swingAnimationTrack = animator:LoadAnimation(swingAnimation)
	swingAnimationTrack:Play()



end)
-- Slash Code // Player // Victim --
handle.Touched:Connect(function(otherPart)
	local char = tool.Parent

	local victimChar = otherPart.Parent
	local victimHumanoid = victimChar:FindFirstChild("Humanoid")
	local victimRootPart = victimChar:FindFirstChild("HumanoidRootPart")
	-- Check if victimChar does not equal wielder char
	if victimChar ~= char then
		if victimHumanoid and victimRootPart then
			-- Check if victim is within hitbox
			local distance = (handle.Position - victimRootPart.Position).Magnitude
			if distance <= hitboxSize then
				-- Deal damage to victim
				if dmgType == "Blunt" then
					victimHumanoid:TakeDamage(dmg)
				elseif dmgType == "Sharp" then
					victimHumanoid:TakeDamage(dmg)
				end
			end
		end
	end			
end)

This is the segment I need help with:

handle.Touched:Connect(function(otherPart)
	local char = tool.Parent

	local victimChar = otherPart.Parent
	local victimHumanoid = victimChar:FindFirstChild("Humanoid")
	local victimRootPart = victimChar:FindFirstChild("HumanoidRootPart")
	-- Check if victimChar does not equal wielder char
	if victimChar ~= char then
		if victimHumanoid and victimRootPart then
			-- Check if victim is within hitbox
			local distance = (handle.Position - victimRootPart.Position).Magnitude
			if distance <= hitboxSize then
				-- Deal damage to victim
				if dmgType == "Blunt" then
					victimHumanoid:TakeDamage(dmg)
				elseif dmgType == "Sharp" then
					victimHumanoid:TakeDamage(dmg)
				end
			end
		end
	end			
end)
1 Like

If HitboxSize is not a Vector3, then comparing a number with a vector3 wouldn’t work. That’s why it pops out the error. You need to make sure the HitboxSize is a Vector3 Value instead of a numbervalue

1 Like
1 Like

Where ever you are getting this error, please use .Magnitude infront of the Vector3 to convert it into a numerical value.

2 Likes

Actually, I just realized. Is hitboxsize a vector3 value?

Yes, yes it is a Vector3 value

You are encountering this error because the hitboxSize is a Vector3, while the distance is just the magnitude between two points, aka just a number.

Just change hitboxSize and you will be good on that part.
However, I would also like to point out the line where you are declaring the distance. When getting the magnitude you are doing the start position minus the target position, usually it would be the other way around, aka (targetPos - startPos).Magnitude

I have never reflected over doing start position minus target position, but the documentation does not mention that magnitude gets the absolute value of the distance, meaning that doing (startPos - targetPos).Magnitude should be able to produce a negative number. I haven’t tested this theory, but based on the documentation it should produce a negative value.

2 Likes

I’m not sure what to do any further as nothing happens. I decided to just make it spawn a cube which will be considered the hitbox, scrapping the script entirely.

That’s why the error is happening. Convert Vector3 to a number value

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.