Stopping a .Touched Function if the person who touched it dies

Hi!

  1. What do you want to achieve? I need to make a .Touched function stop early if the person who touched it dies.

  2. What is the issue? I cannot seem to figure it out.

  3. What solutions have you tried so far? I’ve tried using a connection:Disconnect() in a task.spawn, but that for some reason makes it so the .Touched function only runs one time, and doesn’t work afterwards.

Here’s my code:

script.Parent.Touched:Connect(function(hit)
	if game:GetService("ReplicatedStorage").CurrentCapturePoint.Value == "A" then
		if hit.Parent:FindFirstChild("Humanoid") then
			local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if plr.TeamColor == BrickColor.new("Bright red") and script.Parent.BrickColor ~= BrickColor.new("Bright red") and script.Parent.ParticleEmitter.Enabled ~= true then
				script.Parent.BrickColor = BrickColor.new("Bright red")
				game:GetService("ReplicatedStorage").PointBeingCaptured:FireAllClients('A','Red',true)
				task.spawn(function()
					for i=1,20 do
						if hit.Parent.Humanoid.Health == 0 then
							script.Parent.BrickColor = BrickColor.new("Dark stone grey")
							game:GetService("ReplicatedStorage").PointBeingCaptured:FireAllClients('A','Red',false,true)
							script.Parent.ParticleEmitter.Enabled = false
							script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
						end
						wait(1)
					end
				end)
				script.Parent.ParticleEmitter.Enabled = true
				script.Parent.ParticleEmitter.Color = ColorSequence.new(Color3.fromRGB(255,0,0))
				wait(15)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Enabled = false
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				game:GetService("ReplicatedStorage").CurrentCapturePoint.Value = "B"
				game:GetService("ReplicatedStorage").NumOfCapturePoints.RedPoints.Value += 1

				game:GetService("ReplicatedStorage").PointBeingCaptured:FireAllClients('A','Red',false,false)
				script.Parent.BBG.Enabled = false
				script.Parent.Parent.Parent.CapturePointB.CenterPiece.BBG.Enabled = true
			elseif plr.TeamColor == BrickColor.new("Bright blue") and script.Parent.BrickColor ~= BrickColor.new("Bright blue") and script.Parent.ParticleEmitter.Enabled ~= true then
				script.Parent.BrickColor = BrickColor.new("Bright blue")
				game:GetService("ReplicatedStorage").PointBeingCaptured:FireAllClients('A','Blue',true)
				script.Parent.ParticleEmitter.Enabled = true
				script.Parent.ParticleEmitter.Color = ColorSequence.new(Color3.fromRGB(0,0,255))
				wait(15)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				wait(.5)
				script.Parent.ParticleEmitter.Size = NumberSequence.new(1)
				wait(.5)
				script.Parent.ParticleEmitter.Enabled = false
				script.Parent.ParticleEmitter.Size = NumberSequence.new(.1)
				game:GetService("ReplicatedStorage").CurrentCapturePoint.Value = "B"
				game:GetService("ReplicatedStorage").NumOfCapturePoints.BluePoints.Value += 1
				
				game:GetService("ReplicatedStorage").PointBeingCaptured:FireAllClients('A','Blue',false,false)
				script.Parent.BBG.Enabled = false
				script.Parent.Parent.Parent.CapturePointB.CenterPiece.BBG.Enabled = true
			end
		end
	end
end)

LocalScript for the remote event:

game:GetService("ReplicatedStorage").PointBeingCaptured.OnClientEvent:Connect(function(point,Team,Started,CapturerDied)
	if Started == true then
		script.Parent.Frame.Warning.Visible = true
		script.Parent.Frame.Warning.Text = "<font color='rgb(255,0,0)'>"..Team.."</font> is capturing the point <font color ='rgb(168,66,213)'>"..point.."</font>!"
	else
		if CapturerDied == false then
			script.Parent.Frame.Warning.Text = "<font color='rgb(255,0,0)'>"..Team.."</font> has successfully captured the point <font color ='rgb(168,66,213)'>"..point.."</font>!"
			wait(2)
			script.Parent.Frame.Warning.Visible = false
		else
			script.Parent.Frame.Warning.Text = "<font color='rgb(255,0,0)'>"..Team.."</font> has failed in capturing the point <font color ='rgb(168,66,213)'>"..point.."</font>! Rest assured, they'll come back!"
			wait(2)
			script.Parent.Frame.Warning.Visible = false
		end
	end
end)

Thanks so much!

1 Like

Well, you can do checks and return if the player is dead.

2 Likes

You could add a bool value to the player and check that every time they touch the part.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local AliveValue = Instance.new("BoolValue", player)
	AliveValue.Name = "AliveValue"
	AliveValue.Value = true

	player.CharacterAdded:Connect(function(Character)
		AliveValue.Value = true
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid.Died:Connect(function()
				AliveValue.Value = false
			end)
		end
	end)
end)

Touched script:

local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
	if game:GetService("ReplicatedStorage").CurrentCapturePoint.Value == "A" then
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = Players:GetPlayerFromCharacter(hit.Parent)
			if player:FindFirstChild("AliveValue").Value == false then return end
			
			--the rest of your script here or outside this if statement
		end
	end
end)

I just realized that you could probably just check if the humanoid’s health is set to zero lol

1 Like

Yes, but however it is supposed to stop the function in the middle if the player dies, so I can’t check if they died every time they touch it since they might not be on top of the part when they die.

2 Likes

Potentially, yes, but it would be incredibly inefficient to do that for 20 seconds, lengthening the code so much to the point where it is essentially unreadable if I needed to update it.

1 Like

What even are you trying to do here?

Why does it matter if they touch it while they’re dead?

Add more to the post and provide more context and information so we know why.

Also you can always check if the humanoid health is zero. You can also have a boolean setup as well, or just don’t use .Touched at all and just check if the player is close enough to the center of the part for it to go off.

1 Like

I’m trying to make a system where 2 teams have to fight over a point, and one team must hold said point for 20 seconds to capture it. To stop them from capturing it, you must kill the person who had stepped on it, which is what I’m having issues with here. If they aren’t on top of the part while trying to take it and they die, the part will never know that the died if they need to touch it to die. But the distance would likely work better, so I’ll probably try that one.

1 Like

Use a loop I guess, I don’t know what other method would be possible.

1 Like

Yea try calculating distance from the point. So if there are no players within a certain distance, or whoever is closer can start capturing it. OR you can just implement the bug as the feature, so instead of just having to kill someone to get the point they also have to push to recapture the point which may make for some more intense battles.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.