Weapon Cooldown

This script is supposed to hurt player when the weapon is swung but after swinging the weapon is still able to kill so I tried using connection / disconnect() but now it doesn’t work at all. Could someone please help me?

-- Server Script
local remoteEvent = game.ReplicatedStorage.DamageRM
local coolDown = true
local connection

connection = remoteEvent.OnServerEvent:Connect(function(plr, hit)
		hit.Parent.Humanoid.Health = 0
end)
task.wait(2)
connection:Disconnect()

-- Local Script
local tool = script.Parent.Parent
local myPillowModel = game.Players.LocalPlayer.Character.PillowModel
local damageRM = game.ReplicatedStorage:WaitForChild("DamageRM")
local canDmg = true

local soundService = game:GetService("SoundService")

local humanoid = game.Players.LocalPlayer.Character.Humanoid

--IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
local HitAnimation = Instance.new("Animation")
HitAnimation.AnimationId = "rbxassetid://15910080700"

local HitAnimationTrack = humanoid:LoadAnimation(HitAnimation)

-- IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
local toolRunAnimation = Instance.new("Animation")
toolRunAnimation.AnimationId = "rbxassetid://15910039503"

local toolAnimationTrack = humanoid:LoadAnimation(toolRunAnimation)

local sound = Instance.new("Sound", tool)
sound.SoundId = "rbxassetid://9120386436" 

tool.Equipped:Connect(function()
	myPillowModel.Transparency = 1
	toolAnimationTrack:Play()
end)

tool.Unequipped:Connect(function()
	myPillowModel.Transparency = 0
	toolAnimationTrack:Stop()
end)

tool.Activated:Connect(function()
	if canDmg then
		canDmg = false
		toolAnimationTrack:Stop()
		HitAnimationTrack:Play()
		tool.Handle.Hitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid")then
				damageRM:FireServer(hit)
			end
		
		end)
		task.wait(1)
		toolAnimationTrack:Play()
		task.wait(2)
		canDmg = true
		else
			soundService:PlayLocalSound(sound)
	end
		

	
end)

You should try making your sword script a server script and dealing damage there. A remote event is very insecure for things like this.

The damage IS dealt on the server. The local script is necessary to detect the input of the tool.

You can detect tool input from the server. Will be less prone to exploits and easier to manage since it’s all in one script.

How? I really don’t think that’s possible.

You can use Tool.Activated.

File if you don’t believe me:
tool.rbxm (4.1 KB)

Wow. I never knew that thx but will that fix the problem with the weapon still harming the player after swinging?

Also, I just realized your problem. You have to disconnect the .Touched event too.

Server code:

-- Server Script
local remoteEvent = game.ReplicatedStorage.DamageRM

remoteEvent.OnServerEvent:Connect(function(plr, humanoid)
	humanoid.Health = 0
end)

Client code:

-- Local Script
local tool = script.Parent.Parent
local myPillowModel = game.Players.LocalPlayer.Character.PillowModel
local damageRM = game.ReplicatedStorage:WaitForChild("DamageRM")
local canDmg = true

local soundService = game:GetService("SoundService")

local humanoid = game.Players.LocalPlayer.Character.Humanoid

--IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
local HitAnimation = Instance.new("Animation")
HitAnimation.AnimationId = "rbxassetid://15910080700"

local toolRunAnimation = Instance.new("Animation")
toolRunAnimation.AnimationId = "rbxassetid://15910039503"

local HitAnimationTrack = humanoid.Animator:LoadAnimation(HitAnimation)
local toolAnimationTrack = humanoid.Animator:LoadAnimation(toolRunAnimation)

local sound = Instance.new("Sound", tool)
sound.SoundId = "rbxassetid://9120386436" 

tool.Equipped:Connect(function()
	myPillowModel.Transparency = 1
	toolAnimationTrack:Play()
end)

tool.Unequipped:Connect(function()
	myPillowModel.Transparency = 0
	toolAnimationTrack:Stop()
end)

tool.Activated:Connect(function()
	if canDmg then
		canDmg = false
		toolAnimationTrack:Stop()
		HitAnimationTrack:Play()
		
		local connection = nil
		connection = tool.Handle.Hitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid")then
				damageRM:FireServer(hit.Parent.Humanoid)
			end
		end)
		
		task.wait(1)
		toolAnimationTrack:Play()
		connection:Disconnect()
		
		task.wait(2)
		canDmg = true
	else
		soundService:PlayLocalSound(sound)
	end
end)

By the way, this is really insecure since anyone can send a humanoid to the remote event and kill whoever they want to.

You should also be using .Animator to load animation tracks.

1 Like

Thank you, I will be sure to fix that :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.