Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

This script should be toggled after the humanoid dies, but instead it remains on?

I made a script to test if a toggle mechanism worked. This is what it was:

toggle = false

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			toggle = true
			print("ToggledOn")
			toggle = false
		end)
	end)
end)

if toggle == false then 
	print("ToggledOff")
	
end

so originally toggle is equal to false. If the player dies, then toggle is equal to true which prints ("ToggledOn"). then it is reset, and toggle is equal to false again and it should print ("Toggledoff"). My problem is that it isn't printing Toggled off, only ToggledOn after the humanoid dies. Can someone help me with this?

1 answer

✓ Accepted answer

I just found out that all I had to do was set the bottom part in a run service so that it loops every frame.

toggle = false

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			toggle = true
			print("ToggledOn")
			toggle = false
		end)
	end)
end)

local RunService = game:GetService("RunService")
RunService.Stepped:Connect(function()

if toggle == false then 
	print("ToggledOff")
	
end
end)

1 comment

0

if anyone knows a more optimized way of doing this please lmk

This discussion is preserved as it was. New answers, comments, and votes are closed.