Script works at first then breaks

  1. What do you want to achieve I am making a simple knife script that does damage to people who touch the blade.

  2. What is the issue? It does damage the first time I hit someone but after that it doesn’t work, I think this is because at first the bool is set to true, meaning it works and then somehow it gets set to false and stays false? not sure
    Ive tried a lot of solutions, for hours.

The Local Script:

Damage Script:

Local Script Formatted:

local Knife = script.Parent.Parent
local Blade = Knife.Blade
local Handle = Knife.Handle
local SlashAnim = Knife.Animations.Slash

local EquipSound = Handle.Equip
local HitSound = Handle.Hit
local SlashSound = Handle.Slash
local UnEquipSound = Handle.UnEquip

local CanDamage = script.Parent.Parent.Values.CanDamage.Value

local Cooldown = Knife.Values.Cooldown.Value
local CooldownWait = 1

Knife.Equipped:Connect(function()
	EquipSound:Play()
end)

Knife.Unequipped:Connect(function()
	UnEquipSound:Play()
end)

Knife.Activated:Connect(function()
	local Humanoid = Knife.Parent:FindFirstChildWhichIsA('Humanoid')
	if Humanoid and Cooldown == false then
		Cooldown = true
		CanDamage = true
		local PlaySlashAnim = Humanoid:LoadAnimation(SlashAnim)
		PlaySlashAnim:Play()
		wait(0.3)
		SlashSound:Play()
		wait(CooldownWait)
		Cooldown = false
		CanDamage = false
	end
end)

Damage Script Formatted:

local Knife = script.Parent.Parent
local CanDamage = script.Parent.Parent.Values.CanDamage.Value

Knife.Blade.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and CanDamage == true then
			hit.Parent.Humanoid:TakeDamage(5)
			print("yes")
		else
			print("nos")
			return
		end
		CanDamage = false
end)
1 Like

The server script is not resetting. Try this instead for the script:

local Knife = script.Parent.Parent
local CanDamage = script.Parent.Parent.Values.CanDamage.Value
local CooldownWait = 1

Knife.Blade.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and CanDamage == true then
		CanDamage = false
		hit.Parent.Humanoid:TakeDamage(5)
		print("yes")
		wait(CooldownWait)
		CanDamage = true
	else
		print("nos")
		return
	end
end)
1 Like

I would recommend having CanDamage reference to the bool value itself so you can use it like CanDamage.Value = false, also i see that there isnt anywhere in the script that sets that value back to true

This is because you are changing CanDamage in the client, so the server still see CanDamage value is in false

And other thing, don’t use .Value in the variable because if you are checking it constantly the variable value still in the same value of the start

2 Likes