Debounce until death issue

I want to make a script that if I touch the part it will do the function I want but how would I make it so that it won’t do the action again until the player dies?

I have tried using debounce and humanoid health but it doesn’t work.
Here is the script I’m trying to make the debounce for:

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Arm = Player.Character:FindFirstChild("Left Arm2")
		if Arm then
			Arm.Transparency = 0.9
			wait(0.1)
			Arm.Transparency = 0.8
			wait(0.1)
			Arm.Transparency = 0.7
			wait(0.1)
			Arm.Transparency = 0.6
			wait(0.1)
			Arm.Transparency = 0.5
			wait(0.1)
			Arm.Transparency = 0.4
			wait(0.1)
			Arm.Transparency = 0.3
			wait(0.1)
			Arm.Transparency = 0.2
			wait(0.1)
			Arm.Transparency = 0.1
			wait(0.1)
			Arm.Transparency = 0
		end
    end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Arm2 = Player.Character:FindFirstChild("Right Arm2")
		if Arm2 then
			Arm2.Transparency = 0.9
			wait(0.1)
			Arm2.Transparency = 0.8
			wait(0.1)
			Arm2.Transparency = 0.7
			wait(0.1)
			Arm2.Transparency = 0.6
			wait(0.1)
			Arm2.Transparency = 0.5
			wait(0.1)
			Arm2.Transparency = 0.4
			wait(0.1)
			Arm2.Transparency = 0.3
			wait(0.1)
			Arm2.Transparency = 0.2
			wait(0.1)
			Arm2.Transparency = 0.1
			wait(0.1)
			Arm2.Transparency = 0
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Leg = Player.Character:FindFirstChild("Left Leg2")
		if Leg then
			wait(2.7)
			Leg.Transparency = 0.9
			wait(0.1)
			Leg.Transparency = 0.8
			wait(0.1)
			Leg.Transparency = 0.7
			wait(0.1)
			Leg.Transparency = 0.6
			wait(0.1)
			Leg.Transparency = 0.5
			wait(0.1)
			Leg.Transparency = 0.4
			wait(0.1)
			Leg.Transparency = 0.3
			wait(0.1)
			Leg.Transparency = 0.2
			wait(0.1)
			Leg.Transparency = 0.1
			wait(0.1)
			Leg.Transparency = 0
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Leg2 = Player.Character:FindFirstChild("Right Leg2")
		if Leg2 then
			wait(2.7)
			Leg2.Transparency = 0.9
			wait(0.1)
			Leg2.Transparency = 0.8
			wait(0.1)
			Leg2.Transparency = 0.7
			wait(0.1)
			Leg2.Transparency = 0.6
			wait(0.1)
			Leg2.Transparency = 0.5
			wait(0.1)
			Leg2.Transparency = 0.4
			wait(0.1)
			Leg2.Transparency = 0.3
			wait(0.1)
			Leg2.Transparency = 0.2
			wait(0.1)
			Leg2.Transparency = 0.1
			wait(0.1)
			Leg2.Transparency = 0 
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Torso = Player.Character:FindFirstChild("Torso2")
		if Torso then
			wait(2.7)
			Torso.Transparency = 0.9
			wait(0.1)
			Torso.Transparency = 0.8
			wait(0.1)
			Torso.Transparency = 0.7
			wait(0.1)
			Torso.Transparency = 0.6
			wait(0.1)
			Torso.Transparency = 0.5
			wait(0.1)
			Torso.Transparency = 0.4
			wait(0.1)
			Torso.Transparency = 0.3
			wait(0.1)
			Torso.Transparency = 0.2
			wait(0.1)
			Torso.Transparency = 0.1
			wait(0.1)
			Torso.Transparency = 0 
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Head = Player.Character:FindFirstChild("HeadWhite")
		if Head then
			wait(3)
			Head.Transparency = 0.9
			wait(0.1)
			Head.Transparency = 0.8
			wait(0.1)
			Head.Transparency = 0.7
			wait(0.1)
			Head.Transparency = 0.6
			wait(0.1)
			Head.Transparency = 0.5
			wait(0.1)
			Head.Transparency = 0.4
			wait(0.1)
			Head.Transparency = 0.3
			wait(0.1)
			Head.Transparency = 0.2
			wait(0.1)
			Head.Transparency = 0.1
			wait(0.1)
			Head.Transparency = 0 
			Head:FindFirstChild("Decal").Transparency = 0
			
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local FirstHead = Player.Character.Head:FindFirstChild("face")
		if FirstHead then
			FirstHead.Transparency = 1
		end
	end
end)

Oh no the code it’s not looped properly

Well for one, you probably didn’t check if the Humanoid’s Health would be equal to 0 properly? It should look something like this:

local Part = script.Parent

Part.Touched:Connect(function(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Player then
        local Humanoid = Hit.Parent.Humanoid
        if Humanoid.Health == 0 then
           for Loop = 1, 10 do
               Hit.Transparency -= 0.1
               wait(.1)
           end
        end
    end
end)

Your code is works a bit strange but this should dumb it down?

1 Like

What part can i add this to the script?

You don’t need to add anything, you could literally just try this and see what happens?

it didn’t work. is there anything I could add at the end of each Ontouch?

Like right here?

show24

So if I understand correctly, you want to make it so that a function runs when the player dies? If so, then this is how I would handle it.

local part = script.Parent;
local plrsWhoTouched = {}; --Used to store which players which touched the part.

function makeTransparent(part)
	for i = 0, 1, 0.1 do
		part.Transparency = i;
		wait(0.1);
	end
end

part.Touched:Connect(function(hit)
	local char = hit.Parent;
	local hum = char:FindFirstChild("Humanoid");
	local plr = game.Players:GetPlayerFromCharacter(char);

	if plr and hum and not table.find(plrsWhoTouched, plr) then --Checks if the player didn't touch it before.
		table.insert(plrsWhoTouched, plr); --Adds the player to the list of people who touched the part.
		hum.Died:Wait() --Waits until the player's humanoid dies
		table.remove(plrsWhoTouched, table.find(plrsWhoTouched, plr)); --Removes the player from the list.

		for _, part in next, char:GetChildren() do
			if part:IsA("BasePart") then
				coroutine.wrap(makeTransparent)(part); --Calls the makeTransparent function without a delay between each part.
			end
		end
	end
end)
		
		

No I mean when the player touches the part it runs the whole script using a ontouched event then if the player touches again it wont run the whole script again it will run when he touches the part only if he died

Oh in that case;

local part = script.Parent;
local plrsWhoTouched = {}; --Used to store which players which touched the part.

part.Touched:Connect(function(hit)
	local char = hit.Parent;
	local hum = char:FindFirstChild("Humanoid");
	local plr = game.Players:GetPlayerFromCharacter(char);

	if plr and hum and not table.find(plrsWhoTouched, plr) then --Checks if the player didn't touch it before.
		table.insert(plrsWhoTouched, plr); --Adds the player to the list of people who touched the part.
		hum.Died:Connect(function()
                	table.remove(plrsWhoTouched, table.find(plrsWhoTouched, plr)); --Removes the player from the list.
                end)
		
		--Put code you want to run here.

	end
end)

Mhm. It doesn’t seem to work. If I touch the brick it still will run the script again. I think its because I am Using a StarterCharacter?

To add to this, rather than doing the math for tweening yourself using a for loop I recommend you use TweenService. I’ve compared both before and TweenService is significantly more optimized and overall just looks better. Tweens from TweenService also run on their own threads so there’s no need to create a coroutine. To do your transparency tween:

local goal = { Transparency = 1 };
local tween = game:GetService("TweenService"):Create(part, TweenInfo.new(
1, -- 1 second tween
Enum.EasingStyle.Quad,
Enum.EasingDirection.Out), goal);
tween:Play();

EDIT: Decided to test this just to actually see the difference. Used the same code as above to tween the same part.

image

What exactly are you wanting to do? Make the parts turn visible when the player dies? Or make them invisible?

I’m trying to make a Part where if I touch it it will go to the Players model and get the parts welded in it like (“Left Leg2”) Which I made Left Leg2)And make it visible slowly using wait(0.1) but after the script runs If I touch the Part again. The same visible process will run so the Welded part will be invisible then go visible again. But I want it so you can only touch the part again if the player dies.

Could you try this and see what outputs?

local Part = script.Parent
print("Script running")

Part.Touched:Connect(function(Hit)
    print("Event fired")
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Player then
        print("Found a player")
        local Humanoid = Hit.Parent.Humanoid
        if Humanoid.Health == 0 then
           print("Making the part visible")
           for Loop = 1, 10 do
               Hit.Transparency -= 0.1
               wait(.1)
           end
        else
            print("The humanoid's health is not zero!")
        end
    end
end)

where can I add it? In a separate script or in the same one

It’s literally the same script so the same place

1 Like

It only shows Script running on output. When I touch the part nothing else appears.

Ok chances are the part that you’re referencing is not a valid BasePart variable, can you check your Explorer hierarchy and see what script you put it in is really a BasePart?

If you’re talking about Class Name then its just Part

I put the script in the wrong brick. I placed it in the RightBrick and now on output shows
Event fire
Found a player
the humanoid is health is not 0

But if i touch the brick it still runs the script again