Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I add a distance cap for my grapple script?

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)

1 answer

✓ Accepted answer

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)


1 comment

0

Thank you, This worked very well!

This discussion is preserved as it was. New answers, comments, and votes are closed.