If players dies then disable a function

This script turns on and off a camera lock. There is a problem if the character dies with the tool in his hand it breaks the camera function.

local rs = game:GetService("ReplicatedStorage")
local module = rs:WaitForChild("StrafingController")
local controller = require(module)


script.Parent.Equipped:Connect(function()
	controller:SetEnabled(true)

end)

script.Parent.Unequipped:Connect(function()
	controller:SetEnabled(false)

end)

I need it also to set the controller to false if the player dies. I tried this but it did not work.

local rs = game:GetService("ReplicatedStorage")
local module = rs:WaitForChild("StrafingController")
local controller = require(module)


script.Parent.Equipped:Connect(function()
	controller:SetEnabled(true)
	if script.Parent.Parent.Humanoid.Health<= 0 then
		controller:SetEnabled(false)
	end
end)

script.Parent.Unequipped:Connect(function()
	controller:SetEnabled(false)
	
end)

Anyone know how set this to false if the player dies as well?

Thanks

1 Like

Or also unequipping the tool upon death would solve the issue.

Try this

local rs = game:GetService("ReplicatedStorage")
local module = rs:WaitForChild("StrafingController")
local controller = require(module)

local Tool = script.Parent
local Connection

Tool.Equipped:Connect(function()
	controller:SetEnabled(true)

	local Character = Tool.Parent
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	
	if Connection then
		Connection:Disconnect()
	end

	if Humanoid then
		Connection = Humanoid.Died:Connect(function()
			controller:SetEnabled(false)
		end)
	end
end)

Tool.Unequipped:Connect(function()
	controller:SetEnabled(false)
end)
1 Like

Thanks that fixed the death issue, but it doesn’t disable when the tool is un equipped.

Nevermind I fixed it with one more line. Thanks.