Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

Log in to vote
0
Answered by
rabbi99 714 Moderation Voter
3 years ago

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)


0
Thank you, This worked very well! TypicalWall 17 — 3y
Ad

Answer this question