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

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

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

✓ Accepted answer

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)

4 comments

0

But what about the script.Parent.Equipped on line 9?

0

You cant use .Equipped because it is an event..

0

Still confused, I did the script changes and the same problem is happening. Dont know if im just being stupid

0

Actually wait nevermind, It was something wrong on my end, thank you so much

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