I'm making a flashlight and I want it to turn off when unequipped, but the script won't register when the item is unequipped.

(Script)

Tool = script.Parent
InUse = false
FlashlightMesh = Tool.FlashlightMesh
SpotLight = FlashlightMesh.SpotLight
IdleAnim = script.Idle
RepStorage = game.ReplicatedStorage
Event = RepStorage.Items.FlashLightEvent

Tool.Activated:connect(function()
	print(Tool.Name.. " Activated")
	LightingControl = Tool.Parent:WaitForChild('LightingControl')
	if InUse == false then
		InUse = true
		SpotLight.Enabled = true
		Event:FireAllClients(20,70,false)
		print("1A:")
	elseif InUse == true then
		InUse = false
		SpotLight.Enabled = false
		Event:FireAllClients(20,70,true)
		print("2A:")
	end
end)

Tool.Equipped:connect(function()
	local Humanoid = Tool.Parent:WaitForChild('Humanoid')
	IdleAnimLoaded = Humanoid:LoadAnimation(IdleAnim)
	equipped = true
	IdleAnimLoaded:Play()
end)

Tool.Unequipped:connect(function()
	print("Unequiped")
	equipped = false
	IdleAnimLoaded:Stop()
	LightingControl = Tool.Parent:WaitForChild('LightingControl')
	if InUse == true then
		SpotLight.Enabled = false
		Event:FireAllClients(20,70,true)
		InUse = false
		print("3A:")
	end
end)

(Local Script)

RepStorage = game.ReplicatedStorage
Event = RepStorage.Items.FlashLightEvent
Tool = script.Parent

Event.OnClientEvent:Connect(function(Ambience,FogEnding,On)
	if On == false then
		local Character = Tool.Parent
		local LightingControl = Character:WaitForChild('LightingControl')
		LightingControl.Ambient.Value = LightingControl.Ambient.Value + Ambience
		LightingControl.FogEnd.Value = LightingControl.FogEnd.Value + FogEnding
		print("1B:")
	elseif On == true then
		local Character = Tool.Parent
		local LightingControl = Character:WaitForChild('LightingControl')
		LightingControl.Ambient.Value = LightingControl.Ambient.Value - Ambience
		LightingControl.FogEnd.Value = LightingControl.FogEnd.Value - FogEnding
		print("2B:")
	end
end)

6 comments

1

Put it in your local script

1

Also use local variables.

0

Use a bool value

0

Use a bool value

0

Use a bool value

0

Use a bool value