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

Why does the code try to run even though the tool is not equipped?

Asked by
das1657 20
3 years ago
Edited 3 years ago

So basically, I have a local script running inside a tool to play an animation and fire a remote event whenever I click and have the tool equipped, but instead what happens is that if I click and the tool isn't equipped, it fires the remote event. Could someone tell me why?

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Attack = ReplicatedStorage:WaitForChild("OnAttackSword")
local char = plr.Character


UIS.InputBegan:Connect(function(input, event)
    if input.UserInputType == Enum.UserInputType.MouseButton1 and script.Parent.Equipped then

        print("OK")
        game.ReplicatedStorage:FindFirstChild("OnAttackSword"):FireServer(char)
        script.Parent.Parent.Humanoid.WalkSpeed = 9
        local animation = plr.Character.Humanoid:LoadAnimation(script.Parent.Attack1)
        animation:Play()
        wait(1.3)
        script.Parent.Parent.Humanoid.WalkSpeed = 16

    end
end)

1 answer

Log in to vote
1
Answered by 3 years ago

Inputbegan fires the function whenever the usermade an input. With the if statement, your code fires, every click. This mean the code doesn't check whether the player has equiped the tool or not. There is actually a special event made for this that fires a function whenever the tool is equiped (.Activated)

local Tool = script.Parent
Tool.Activated:Connect(function()
    -- your code
end)
0
But what about the script.Parent.Equipped on line 9? deeskaalstickman649 475 — 3y
0
You cant use .Equipped because it is an event.. JustinWe12 723 — 3y
0
Still confused, I did the script changes and the same problem is happening. Dont know if im just being stupid das1657 20 — 3y
0
Actually wait nevermind, It was something wrong on my end, thank you so much das1657 20 — 3y
Ad

Answer this question