Boolean value not changing

I made a Boolean value inside starter character scripts called canDamage. Basically im making a punch script and this value is supposed to determine if the humanoid can be damaged.

The Boolean Value can change to true but it cant be changed to false.

Here is my code

local candamage = script.Parent.canDamage --Boolean Value

script.Parent.RightHand.Touched:Connect(function(part)
	local hum = part.Parent:FindFirstChild("Humanoid")
	if hum and candamage.Value then
		hum.Health = hum.Health - 15
	end
end)

script.Parent.LeftHand.Touched:Connect(function(part)
	local hum = part.Parent:FindFirstChild("Humanoid")
	if hum and candamage.Value then
		hum.Health = hum.Health - 15
	end
end)

script.Parent.RightFoot.Touched:Connect(function(part)
	local hum = part.Parent:FindFirstChild("Humanoid")
	if hum and candamage.Value then
		hum.Health = hum.Health - 15
	end
end)

script.Parent.LeftFoot.Touched:Connect(function(part)
	local hum = part.Parent:FindFirstChild("Humanoid")
	if hum and candamage.Value then
		hum.Health = hum.Health - 15
	end
end)

I am desperate for help.

The code itself is fine but you are not setting the boolean anywhere in the script. So it will always be true/false (depending on what its set to before you play the game)

No im setting the value in other scripts.

Are both the scripts (the one that sets the boolean value and the script in your post) the same type? So are they both for example a Server script

yes they both are here is the other script:

local anim = script:FindFirstChild('punch')

local humanoid = script.Parent:WaitForChild('Humanoid')

local punch = humanoid:LoadAnimation(anim)

script.Parent.punchevent.OnServerEvent:Connect(function()
	script.Parent.canDamage.Value = true
	script.Parent.Punch:Play()
	punch:Play()
	script.Parent.triggerPunchshake:FireClient()
	script.Parent.canDamage.Value = false
end)

Try adding a wait(x) before

Right now you set it “immediately” after setting it to true back to false

1 Like

It didnt help solve the problem, but it helped the punching system. I found out the reason why it wasnt setting back to false was because the script was getting stuck on this line:

	script.Parent.triggerPunchshake:FireClient()

The event wasnt firing for some reason so the script got stuck there.
Thanks for the feedback anyways.

Weird that it got stuck on that line. Only RemoteFunctions should be able to yield scripts.

But glad to hear it works! :happy1:

1 Like