Area Detection cooldown + kill script

No that end is for the countdown loop if you put the killing inside the loop then it will kill after the first second aka every time the loop runs.

That should work. I’m sorry but I have to go now!

Now it works perfectly besides this one issue:

I do appreciate the amount of help you’ve given, its really helped out. And anyone else that helped too <3

script.Parent.Touched:Connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p then
		local ui = p.PlayerGui.Cold.Frame
		ui.Visible = true
		if ui.Visible then
			for i = 10, 0, -1 do 
				ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
				wait(1)
			end
			hit.Parent.Humanoid.Health -= 500
		else
			ui.TextLabel.Text = "Dangerous area turn around! - 10s"
		end
	end
end)



Issue is

I have time for one more reply make sure to add a check to check if the player is still touching the part

Ok I will try finding out how linking back to cooldowns:
OPEN GUI with a PART - Roblox Scripting Tutorial - YouTube

Use touchended and have variable called touching when the player touches set touching to true when touchended set touching to false

Yes.

Fixed the stacked issue:
However I am still confused on how to detect when players are no longer on the part, so if anyone can help me detect when users are off the part then end the kill script, that would be appreciated. Unless I need to revert back to the singular typing out and detected if p is false?

local debounce = true
local par = script.Parent
script.Parent.Touched:Connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p then
		local ui = p.PlayerGui.Cold.Frame
		if debounce and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		debounce = false
		ui.Visible = true
		if ui.Visible then
			for i = 10, 0, -1 do 
				ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
				wait(1)
			end
			hit.Parent.Humanoid.Health -= 500
			wait(1)
			debounce = true
			ui.Visible = false
		else debounce = true
			ui.TextLabel.Text = "Dangerous area turn around! - 10s"
			ui.Visible = false
			end
		end
	end
end)

Issue is:

  • The UI doesn’t stop when the humanoid is off the kill part.

So what I was saying setup a variable called touching:

local touching = false

When you touch the part set touching to true

Then setup a touch ended:

script.Parent.TouchEnded:Connect(function()
    touching = false
end)

Then in the loop in the touched event add a check to check if touching is false
So:

for i = 10, 0, -1 do 
    ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
    if not touching then break end
    wait(1)
end

Final:

local touching = false

script.Parent.Touched:Connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p then
		local ui = p.PlayerGui.Cold.Frame
		if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		touching = true
		ui.Visible = true
		if ui.Visible then
			for i = 10, 0, -1 do 
				ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
                if not touching then break end
				wait(1)
			end
			hit.Parent.Humanoid.Health -= 500
			ui.Visible = false
		else
			ui.TextLabel.Text = "Dangerous area turn around! - 10s"
			ui.Visible = false
			end
		end
	end
end)

script.Parent.TouchEnded:Connect(function()
    touching = false
end)

I mean I made one called ‘debounce’ instead, could I put:

script.Parent.TouchEnded:Connect(function()
   touching = false
end)

Before line 2?
image

I’m not really sure what your asking but try what I wrote because I just edited the reply

After testing it…


It killed me instantly on touch.

local touching = false
script.Parent.Touched:Connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p then
		local ui = p.PlayerGui.Cold.Frame
		ui.Visible = true
		if ui.Visible then
			for i = 10, 0, -1 do 
				ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
				if not touching then break end
				wait(1)
			end
			hit.Parent.Humanoid.Health -= 500
		else
			ui.TextLabel.Text = "Dangerous area turn around! - 10s"
		end
	end
end)

Because you never set touching to true so it immediately broke out of the loop and killed you.

Also instead of subtracting 500 from the Heath just set the health to 0

I put the new script at the end of the last post btw

image
So using this new script you provided:

local touching = false

script.Parent.Touched:Connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p then
		local ui = p.PlayerGui.Cold.Frame
		if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
			touching = true
			ui.Visible = true
			if ui.Visible then
				for i = 10, 0, -1 do 
					ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
					if not touching then break end
					wait(1)
				end
				hit.Parent.Humanoid.Health -= 500
				ui.Visible = false
			else
				ui.TextLabel.Text = "Dangerous area turn around! - 10s"
				ui.Visible = false
			end
		end
	end
end)

script.Parent.TouchEnded:Connect(function()
	touching = false
end)

It would appear that you die instantly after stepping off the part.

Error mess: image
Also thank you so much for all the help <3 it really means a lot that you’re going the extra mile.

That’s because after we break out of the loop we kill the player. Let me think for a minute

I guess you can make a should kill variable and set it to true when the loop successfully finishes and set it to false if we break out so like this:


local touching = false
local shouldKill = false

script.Parent.Touched:Connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p then
		local ui = p.PlayerGui.Cold.Frame
		if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
			touching = true
			ui.Visible = true
			if ui.Visible then
				for i = 10, 0, -1 do 
					ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
					if not touching then             
                        shouldKill = false
                        break 
                    else
                        shouldKill=true
                    end
					wait(1)
				end
				if shouldKill then hit.Parent.Humanoid.Health = 0 end
				ui.Visible = false
			else
				ui.TextLabel.Text = "Dangerous area turn around! - 10s"
				ui.Visible = false
			end
		end
	end
end)

script.Parent.TouchEnded:Connect(function()
	touching = false
end)

It bloody works like a charm now! Thank you so much for all your help and Fakies simplified method.
This means a lot and will help me progress, I’m sorry this took like 2 hours. :sob:. <333333333