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

I have a server script and local script and the local script fires a remote event when the players mouse is held down. However, the server script damages other players even when someone is not holding the mouse down and keeps repeating it so whenever the handle is touched, it 80% of the time insta kills the player it touched. Is there a fix for this?

This is the server script

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

meleeEvent.OnServerEvent:Connect(function()
	script.Parent.Handle.Touched:Connect(function(otherPart)
		local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			wait(0.45)
			humanoid:TakeDamage(damageamount)
		end
	end)
end)
2 Likes

Maybe you can add an cooldown, for example.

local meleeEvent = script.Parent:WaitForChild("MeleeEvent")
local damageamount = 25
local cooldown = false

meleeEvent.OnServerEvent:Connect(function()
	script.Parent.Handle.Touched:Connect(function(otherPart)
		local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
                    if cooldown == false then
                        cooldown = true -- No more damage
			wait(0.45)
			humanoid:TakeDamage(damageamount)
             wait(1) -- reloads
            cooldown = false --  the system repeats if touched
            end
		end
	end)
end)

Thanks! This fixed the insta kill problem, but it didn’t fix the main issue

Is there a way around that because I can’t use player:GetMouse() for a server script

Can you show your Local Script?

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)

mouse.Button1Down:Connect(function()
	meleeing = true
	while equipped and meleeing do
		meleeEvent:FireServer()
		hittrack:Play()
		sound:Play()
		wait(0.8)
		mouse.Button1Up:Connect(function()
			meleeing = false
		end)
	end
end)
1 Like

So, it kills the player that is holding the weapon too, right?

No, it doesn’t do damage to the player holding the weapon only the player the handle touches which isn’t holding the tool

I think i understand, Âżit hurts the other player, even if the player that is holding the weapon is not doing it?

If you meaning clicking the mouse, then yes

show an photo of the Weapon Model (tool)

Does it give players damage if you have not clicked yet?

if so maybe it is because the touched event keeps the function it is connected to

maybe make a variable called hasClicked below “local damageamount = 25” and make it false
then upon the OnServerEvent make it true and then wait for the amount you want to make it false

then place the current touched event outside of the onServerEvent and create an if statement to see if the player has clicked
and then allow it to damage

local meleeEvent = script.Parent:WaitForChild(“MeleeEvent”)
local damageamount = 25
local hasClicked = false

meleeEvent.OnServerEvent:Connect(function()
if hasClicked then
hasClicked = true
wait(.25)
hasClicked = false
end
end)

script.Parent.Handle.Touched:Connect(function(otherPart)
if hasClicked then
local humanoid = otherPart.Parent:FindFirstChildOfClass(“Humanoid”)
if humanoid then
wait(0.45)
humanoid:TakeDamage(damageamount)
end
hasClicked = false
end
end)

It is a shovel that is slightly bigger that a players hand

1 Like

Yes thats what the main problem is

Thanks but all that did was make the shovel do no damage at all

Try this, maybe an cooldown in the LocalScript

	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
	local HasClicked = false

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

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

	mouse.Button1Down:Connect(function()
	if not HasClicked then
		HasClicked = true
			meleeEvent:FireServer()
			hittrack:Play()
			sound:Play()
			wait(0.8)
		 mouse.Button1Up:Connect(function()
			 HasClicked = false
		 end)
     end
end)
1 Like

The problem is not with the local script because it is not the script that does damage

local meleeEvent = script.Parent:WaitForChild("MeleeEvent")
local damageamount = 25
local cooldown = false

meleeEvent.OnServerEvent:Connect(function()
	if cooldown == false then
	script.Parent.Handle.Touched:Connect(function(otherPart)
		local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
		cooldown = true
		if humanoid then
				wait(0.45)
				humanoid:TakeDamage(damageamount)
				wait(1) -- reloads
				cooldown = false --  the system repeats if touched
			end
		end)
	end
end)

And it made the shovel not automatic but instead I have to keep clicking it

Is there a particular reason why you used a local script?