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

How can i do a action but only if there is a part with a specific name in the radius?

Asked by
Shematics 117
4 years ago
Edited 4 years ago

Hi i wish to make a ledge grabbing script, i already done that when i click E it does a animation, now i wish to make it that when i click E it will execute the animation BUT only if there is a part called "grab" near in the radius so the animation will execute really near or even on the "grab" part location so it can adjust itself

is that possible?

Here is the script:


local UserInputService = game:GetService("UserInputService") local climbing = false local Player = game.Players.LocalPlayer local Character = game.Workspace:WaitForChild(Player.Name) local Humanoid = Character:FindFirstChildOfClass("Humanoid") Climb = Instance.new("Animation") Climb.AnimationId = "rbxassetid://3284607610" ClimbTrack = Humanoid:LoadAnimation(Climb) UserInputService.InputBegan:Connect(function(Input) if Input.KeyCode == Enum.KeyCode.E and not climbing then Climbing = true ClimbTrack:Play() Climbing = false end end)
0
yes Sametics -4 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Enfaite tu va creer une variable dans ton script, je l'appelle Active. C'est un booleen, vrai ou faux.

local Active = false

Maintenant on va créer une répétition avec while. On va utiliser magnitude poyr trouver la distance entre l'objet et le joueur.

local Active = false

while wait() do
      Active = false
      local mag = (part.Position - Character.HumanoidRootPaet.Position).magnitude
end

Tu va déterminer la distance entre l'object et le joueur.

local Active = false

while wait() do
      Active = false
      local mag = (part.Position - Character.HumanoidRootPaet.Position).magnitude
      if mag < 5 then

      end
end

Maintenant tu va changer la valeur de Active sur vrai

local Active = false

while wait() do
      Active = false
      local mag = (part.Position - Character.HumanoidRootPaet.Position).magnitude
      if mag < 5 then
           Active = true
      end
end

Ajoute cette ligne dans ton Userinput


UserInputService.InputBegan:Connect(function(Input) if Input.KeyCode == Enum.KeyCode.E and not climbing and Active then Climbing = true ClimbTrack:Play() Climbing = false end end)
Ad
Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago

Here's what I would do. With a model of parts which include the "grab" parts I would run a function which would go through each one and check to see if it was near the player.

If there are many 'grab' parts in this model, you could create a way to separate which parts the function runs on. But in most cases we won't need this.

local UserInputService = game:GetService("UserInputService")

local climbing = false

local Player = game.Players.LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

--my variables
local armLength=3 --length of radius
local grabParts=workspace.grab --change to model of grab parts

--making grab check function
function grabRangeCheck()
    --go through each grab part
    for _,grab in pairs(grabParts) do
        --if close by then return the grab part
        if (grab.Position-Character.HumanoidRootPart.Position).Magnitude<=armLength then
            return grab
        end
    end
end



Climb = Instance.new("Animation")
Climb.AnimationId = "rbxassetid://3284607610"
ClimbTrack = Humanoid:LoadAnimation(Climb)

UserInputService.InputBegan:Connect(function(Input)
    --added range check function
    local grab=grabRangeCheck()
    if Input.KeyCode == Enum.KeyCode.E and not climbing and grab then
        Climbing = true
        ClimbTrack:Play()
        Climbing = false
    --run extra code for what to do with the grab part under variable "grab"
    end
end)

And tada! The animation will only run if a grab part is nearby.

Extra

If there are a lot of grab parts you could create a parameter for the check function to run the for loop for a certain 'map'

grabRangeCheck(mapname)

and in the function

for _,grab in pairs(>mapname<) do

Answer this question