Area Detection cooldown + kill script

  1. Goal: Attempting to make it so, when you are touching certain parts in a folder you get a UI saying “Dangerous area turn around! - 10s” Then after 10 seconds it kills you. - So you are in an ‘area part’ and if you stay in it when the text label says 1s you get killed, but if you leave the area the ui sets back to 10s and become invisible. (visible = false)

  2. What is the issue?image

  3. What solutions have you tried so far?
    OPEN GUI with a PART - Roblox Scripting Tutorial - YouTube
    How to make a Popup GUI | Roblox Studio (check pinned comment for script) - YouTube

Workspace:
image
StarterGui:
image

local ui = game.StarterGui.Cold.Frame.Visible
local p = game.Players.LocalPlayer
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local StarerGui = p:GetService("StarterGui")
		game.StarterGui.Cold.Frame.Visible = true
		if ui == true then
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 9s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 8s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 7s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 6s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 5s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 4s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 3s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 2s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 1s"
			p.Humanoid.Health -= 500
		elseif ui == false then
			game.StarterGui.Cold.Frame.TextLabel.Text = "Dangerous area turn around! - 10s"
		end
	end
end)

Any kind of guidance/help is really appreciated, I’ve been stuck on this for a long time.

1 Like

You are trying the get the StarterGui Service from the player which doesn’t make sense

Also you should be using player.PlayerGui and not StarterGui.

In your case

local frame = p.PlayerGui.Cold.Frame
frame.Visible = true

Then perform all your things using

if frame.Visible then

end

Also your your variable references the visibility so you are trying to do

game.StarterGui.Cold.Frame.Visible.TextLabel.Text = "1s"

You’re trying to get the service from the player?

game:GetService("StarterGui")

Based on what I previously said

Try this script:


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

One more question is this a local or server script.

1 Like
  1. Use task.wait(). Roblox is planning to deprecate wait() + task.wait() is more exact.
  2. Use a loop. This…
            wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 9s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 8s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 7s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 6s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 5s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 4s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 3s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 2s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 1s"

… can be shortened to this …

for i = 10, 0, -1 do 
    ui.TextLabel.Text = "Dangerous area turn around! - "..i.."s"
    task.wait(1)
end
  1. It is recommended to use full words as variables. It’s bad practice if you use just a character.
  2. You’re doing something weird. And you have a typo. If you want to get the player’s gui, you have to do this:
local PlayerGui = p.PlayerGui
PlayerGui.Cold.Frame.Visible = true
1 Like

The script should be local and effecting one person only. There’s an issue with your script:
image

local p = game.Players.LocalPlayer
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local ui = p.PlayerGui.Cold.Frame -- LINE 4
		if ui.Visible == true then
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 9s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 8s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 7s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 6s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 5s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 4s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 3s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 2s"
			wait(1)
			ui.TextLabel.Text = "Dangerous area turn around! - 1s"
			p.Humanoid.Health -= 500
		elseif ui == false then
			game.StarterGui.Cold.Frame.TextLabel.Text = "Dangerous area turn around! - 10s"
		end
	end
end)```

If it’s a server script the issues is you can’t use local player

Try the new updated script it should work now (unless it’s not a local script)

My bad change player to p and remove the then. I edited the script so now it should actually work (sorry about the mistakes written on mobile!) so try out the new script

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

Maybe you’ll try this

So, after fixing the errors and implementing the new script this is what happens:

  • No errors
  • Text label & frame doesn’t show.
    (Changed to local script)

You can have it so that whatever you do to the part, and when you want the UI to appear, just fire a RemoteEvent.

This won’t work because of this line:

This is very similar to the updated version of my script which has all the necessary changes

Where is the script located

If it’s in a part like I think then a local script itself won’t work you will need to use a remote event

Local scripts don’t work outside of StarterGui and Playerscripts. You need to use a RemoteEvent to have this properly work.

Going off what Zayd and you have said so far I’ve got this:

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

So now we need to fire a remote event that does what we need to do now?
image
image

(Look 2 posts below for the easy 1 script way)

So:

  • Make a remote event in ReplicatedStorage (for example named showUi)
  • Add a Local Script in StarterGui (basically the same script but instead of the touched event put it in a game.ReplicatedStorage.showUi.OnClientEvent:Connect(function(player)
  • In your part make a Script (server) and put this in it:
local event = game:GetService("ReplicatedStorage").showUi

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        event:FireClient(plr)
    end
end)

If you don’t want to use a remote event you have another (easier) option let me know if you want me to tell you