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)
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)
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)
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)
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)
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)