Hello, i am trying to make a grappling hook, and i have managed to get everything working except for one thing, i cannot figure out how far away the cursor is from the player in the 3D space. How can i set a range to my grappling hook?
Here’s my script
script.GrappleOn.OnServerEvent:Connect(function(player, mousepos, grappling)
if grappling == false then
--print("grappling: " ..tostring(grappling)) --debug
if player.Character:FindFirstChild("SpringConstraint") then
player.Character:FindFirstChild("SpringConstraint"):Destroy()
workspace:FindFirstChild("grapplepart"):Destroy()
end
return
end
--ref
local char = player.Character
local hrp = char.HumanoidRootPart
-- vector3
local maxdist = Vector3.new(700, 700,700)
local dist = mousepos.Magnitude
--[[ debug
print(dist)
print(maxdist)
print("grappling: " ..tostring(grappling))
--]]
if grappling == true then
if dist <= maxdist.x and dist <= maxdist.y and dist <= maxdist.z then
local humanoidattachment = Instance.new("Attachment")
local grapple = Instance.new("SpringConstraint")
local targetattachment = Instance.new("Attachment")
local grapplepart = Instance.new("Part")
local grappleProperties = {
Thickness = 0.3,
Coils = 10,
Parent = char,
Attachment0 = targetattachment,
Attachment1 = humanoidattachment,
Visible = true,
FreeLength = 2,
Stiffness = 5000,
MaxForce = 6500
}
local grapplepartProperties = {
Parent = workspace,
Anchored = true,
Position = mousepos,
CanCollide = false,
Transparency = 1,
Name = "grapplepart"
}
for x, v in pairs(grappleProperties) do
grapple[x] = v
end
for x, v in pairs(grapplepartProperties) do
grapplepart[x] = v
end
humanoidattachment.Parent = char:FindFirstChild("UpperTorso")
humanoidattachment.Name = "humanoidattachment"
targetattachment.Parent = grapplepart
end
end
end)