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)