How do I make this script only activate when a player clicks while holding a weapon

Do you think I should put takedamage in the local script or will that not work?

This is super cool! Sorry with the many questions but again out of curiousity, the “:Wait()” just to be sure that i understand, it is simply like the normal wait() so all the code below it wont run until the wait is over for the button1up to occur right?

Maybe try duplicate your current tool and remove the local script and remote event (it may not be that there is something wrong with it but if it can be done with one script) then implement:

which should give you the result of the player only taking damage upon the mouse button being held down

from there you could add in your animations and everything else and piece it together!

For the animation I would need a local script to get the players character though and when I try this

local hit = script:WaitForChild("Hit")
local player = tool.Parent
local humanoid = player:WaitForChild("Humanoid")

local hittrack = humanoid:LoadAnimation(hit)

It says in the output Infinite yield possible on ‘Players.(PlayerNameHere).Backpack:WaitForChild(“Humanoid”)’

btw, you can send info through remote events. As if they are a function.
so on the client:

event:FireServer("Bing Bong")

and on the server:

event.OnServerEvent:Connect(function(Player, String)
    print(String)
end)

The reason for the player variable is that the event always passes what player fired it.
output would be something like:

server - "Bing Bong"

this means you can pass what GetMouse() returns to the server.
this doesn’t really relate to your problem but I thought you might need to know this.

I think I may be able to use that to fix the issue so it detects when the mouse clicks.

Actually in the tool script you can get the character.
because if the tool is in their backpack, then it’s in the backpack of their Player object,
but if it’s equipped, then it’s under their character object.
there’s a tool.Equipped event, so you could use code like this:

local tool = script.Parent
local Character

tool.Equipped:Connect(function()
     Character = tool.Parent
end)

with the character object, you can do all the animation stuff you need.
and equipped should happen before anything else happens in here.
another way to get the character probably more reliably would be to:

local Tool = script.Parent
local Player = Tool.Parent.Parent
local Character = Player.Character

this will work as long as you don’t immediately add the tool to the character instead of the backpack.
there are other, more complicated ways of doing this that don’t break but one of these should work for you.

Yep. I think its better to yield if you are expecting that event.to fire as you only need to have 1 function and connection instead of 2. plus it looks a lot neater imo

1 Like

Since RemoteEvents always pass the plr instance when done, you could get the players character and check if the tool is in their avatar and if it is then do that and if it isn’t then don’t; unless this is under the tool.

local meleeEvent = script.Parent:WaitForChild("MeleeEvent")
local damageamount = 25
local tool = script.Parent
local handle = tool.Handle

local debounce = false

meleeEvent.OnServerEvent:Connect(function(plr)
	local character = plr.Character
	if tool.Parent == character then
		handle.Touched:Connect(function(otherPart)
			local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
			if humanoid and debounce == false then
				debounce = true
				wait(0.45)
				humanoid:TakeDamage(damageamount)
				debounce = false
			end
		end)
	end
end)

Edit: while working on this someone already replied rip

for the part to make it work only when a player clicks while holding a weapon maybe disable the script and then when the player equips the tool enable the script? i don’t relly kno how to script so i cant make a script like other people do, but it seems you can figure it out?

I think I can make it so when it is clicked then the script is enabled

1 Like

I don’t think that would work because it would click and try to enable the script at the same time, it might cause some issues depending on which one goes first or something, I don’t know. You probably know more, so I guess go for it.

I don’t think so as a script I use for rounds does that same thing but it doesn’t give any errors

1 Like

alright then, go for it! good luck

instead of mouse.Button1Down you should use tool.Activated, this only fires when the player clicks with the tool equipped, mouse.Button1Down fires everytime a player clicks.

Your new local script would become

local tool = script.Parent
local hit = script:WaitForChild("Hit")
local sound = script:WaitForChild("HitSound")
local player = game.Players.LocalPlayer
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local hittrack = humanoid:LoadAnimation(hit)
local meleeEvent = script.Parent:WaitForChild("MeleeEvent")

local meleeing = false
local equipped = false

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

tool.Activated:Connect(function()
	meleeing = true
	while equipped and meleeing do
		meleeEvent:FireServer()
		hittrack:Play()
		sound:Play()
		wait(0.8)
		tool.Deactivated:Connect(function()
			meleeing = false
		end)
	end
end)
2 Likes