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

How to find and set tool in script? (read desc)

Asked by 4 years ago

Tool's name is "Excalibur", parent "Workspace". But how to put that in script? I tried local Tool = script.Parent.Workspace and local Tool = Excalibur and many other things, but nothing worked. (my scripting skills are almost zero)

-- R attack
local Tool =         -- here
local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local AnimationID = 'rbxassetid://4868889544'
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationID
local LoadAnimation = Humanoid:LoadAnimation(Animation)
local key = 'R'
local debounce = true

Tool.Equipped:Connect(function() 
    UserInputService.InputBegan:Connect(function(Input, IsTyping) 
        if  IsTyping then return end
        if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
            debounce = false
            LoadAnimation:Play()
            wait(5)
            debounce = true
        end
    end)
end)

Tool.Unequipped:Connect(function() 
    LoadAnimation:Stop()
end)

2 answers

Log in to vote
0
Answered by 4 years ago

Identifying a tool anywhere is supper easy! All you have to do is check for the tool in workspace, for example;

local Tool = game:GetService("Workspace"):FindFirstChild("Excalibur")

See how we identified where it's located (Workspace) and then used FindFirstChild to check for the tool! Make sure that you're checking after the tool has loaded. If the script instantly checks, you may need to wait for the tool to load.

For this, you could do many things. One example would be:

local Tool = game:GetService("Workspace"):WaitForChild("Excalibur")

But keep in mind the WaitForChild() can yield, and it's expensive until it locates the tool

Ad
Log in to vote
0
Answered by
P100D 590 Moderation Voter
4 years ago

To find an instance that is the child of another instance, use :FindFirstChild()

local tool = game.Workspace:FindFirstChild("Excalibur")

This will return the first child of Workspace with the given name, or nil if none such instance exists.

For more info, see the relevant documentation.

Answer this question