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

Why is my button1down not detecting the click?

So i have this script that is a tool. This script is a server script.

local player = script.Parent.Parent.Parent
local char = player.Character
local knife = script.Rock
local holdan = script.Hold
local idle = script.Idle
local hit = script.Hit
wait(1)
local hold = char.Humanoid:LoadAnimation(holdan)
local idlepla = char.Humanoid:LoadAnimation(idle)
local hit = char.Humanoid:LoadAnimation(hit)
local hitting = false
script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:connect(function()
		print(mouse.Parent.Name)
		print("yes")
		if hitting == false then
			hitting = true
			idlepla:Stop()
			hit:Play()
			wait(1)
			idlepla:Play()
			hitting = false
		end
	end)
	char.Humanoid:AddAccessory(knife:Clone())
	hold:Play()
	wait(0.46)
	idlepla.Looped = true
	idlepla:Play()
end)
script.Parent.Unequipped:Connect(function()
	idlepla:Stop()
	char.Rock:Destroy()
end)

That is the whole code which is supposed to play animations

script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:connect(function()
		print(mouse.Parent.Name)
		print("yes")
		if hitting == false then
			hitting = true
			idlepla:Stop()
			hit:Play()
			wait(1)
			idlepla:Play()
			hitting = false
		end
	end)

this part is not working as the "mouse.Button1Down:connect(function()" part is not detecting the click. Please help me.

1 comment

0

What is this script parented to?

1 answer

First, you need to call the mouse. To do this, you need mouse = players.LocalPlayer:GetMouse()

When the sword is equipped, the click function runs only every time the sword is equipped. Since the button1down function is written inside the equip function, once the equip function runs, unless there is some sort of loop involved, the code inside the equip function will only run once. Essentially, your mouse function will run only right after the equip function, and only once and then the whole thing ends.

In short, you need to take out the mouse.button1down function outside of the equip function, otherwise, it will check for clicks before you can even attempt to click at all.

Perhaps a solution would be having three separate functions and one bool value.

Create a boolean value variable named "equipped", preferably with that script, or just make a boolean value in the workspace, also name it equipped. Set that boolean value's value to false.

Create a function that triggers when you equip the tool. Write an if statement that checks if the equipped value is set to false. If so, set equipped to true. and put whatever knife cloning or other code you have also inside that equip function

Below that, write a function that triggers when the tool is unequipped. Write an if statement that checks if the equipped value is set to true. If so, set equipped to false. and put whatever knife stuff or other code you have also inside that unequip function

Now, at the very bottom write this:

mouse.Button1Down:Connect(function()
	if hitting == false then
		hitting = true
		idlepla:Stop()
		hit:Play()
		wait(1)
		idlepla:Play()
		hitting = false
	end
end)

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