I'm trying to make a distance cap for my grapple script and I'm stuck
The script
local UIS = game:GetService("UserInputService") local hum = script.Parent.Parent.Humanoid local char = script.Parent.Parent local mouse = game.Players.LocalPlayer:GetMouse() local gt = Instance.new("Animation") gt.Name = "GrappleThrow" gt.AnimationId = "rbxassetid://MyAnimId" local gp = Instance.new("Animation") gp.Name = "GrapplePull" gp.AnimationId = "rbxassetid://MyAnimId" local gt1 = hum:LoadAnimation(gt) local gp1 = hum:LoadAnimation(gp) UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.Q then if mouse.Target and script.cooldown.Value == false then if mouse.Target.Name == "GrapplePoint" then local mod = mouse.Target script.Parent.GrappleEvent:FireServer(mod) gt1:Play() wait(0.4) gp1:Play() wait(0.4) end end end end)
Simple! All you need is a MaxDistance variable and an if-statement.
local UIS = game:GetService("UserInputService") local hum = script.Parent.Parent.Humanoid local char = script.Parent.Parent local mouse = game.Players.LocalPlayer:GetMouse() local gt = Instance.new("Animation") gt.Name = "GrappleThrow" gt.AnimationId = "rbxassetid://MyAnimId" local gp = Instance.new("Animation") gp.Name = "GrapplePull" gp.AnimationId = "rbxassetid://MyAnimId" local gt1 = hum:LoadAnimation(gt) local gp1 = hum:LoadAnimation(gp) local MaxDistance = 100 UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.Q then if mouse.Target and script.cooldown.Value == false then if mouse.Target.Name == "GrapplePoint" and (mouse.Target.Position - char.Head.Position).Magnitude < MaxDistance then local mod = mouse.Target script.Parent.GrappleEvent:FireServer(mod) gt1:Play() wait(0.4) gp1:Play() wait(0.4) end end end end)