How do I update player GUI when they enter safe zone

I’m trying to make gui show when player enter safe zone

local part = script.Parent
local region = Region3.new(part.Position - part.Size/2, part.Position + part.Size/2)

local rp = game:GetService("ReplicatedStorage")
local remoteF = rp.showGui

part.Size = region.Size
part.Anchored = true
part.CanCollide = false
local ignore = {workspace.Baseplate, part, workspace.SpawnLocation}

while true do
	wait()
	

	part.Size = region.Size

	local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, ignore, math.huge)

	if #partsInRegion ~= 0 then
		for i,v in pairs(partsInRegion) do
			local humanoid = v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid")
			if humanoid then
				remoteF:FireClient(humanoid.Parent)
				print(humanoid.Parent)
				humanoid.HealthChanged:Connect(function(hp)
					humanoid.Health = 100
					wait(0.1)
				end)
			end
			
		end


	end

end

Thank for helping :grin:

I’m guessing this supposed to make gui update but :FireClient() takes a player argument, not character. To get player from character you can use game.Players:GetPlayerFromCharacter() so you should do this to fix that:

local player = `game.Players:GetPlayerFromCharacter(humanoid.Parent)
remoteF:FireClient(player)
1 Like
  1. Make a safe zone GUI
  2. Move the GUI to ServerStorage
  3. If player’s character has touched the region and the player doesn’t have the safe zone GUI: clone the GUI from ServerStorage and set Parent property of the GUI to player.PlayerGui

Inform me about having any issue(s) with any of the steps above.

1 Like

what about destroy where can I put destroy

Do you mean destroying the GUI if player’s character leaves the safe zone?

1 Like

yes I don’t know how do I put it some time when it will error when the gui isn’t declare yet

Forgot to mention that FireClient() is not needed here to give the GUI.

Yes I delete it already :grin:

Modify your region script to make a table of players inside the region for every region update:

local part = script.Parent
local region = Region3.new(part.Position - part.Size/2, part.Position + part.Size/2)

local rp = game:GetService("ReplicatedStorage")
local remoteF = rp.showGui

part.Size = region.Size
part.Anchored = true
part.CanCollide = false
local ignore = {workspace.Baseplate, part, workspace.SpawnLocation}

while true do
	wait()
	local playersInRegion = {} -- The script should make a table of players, who are inside the region

	part.Size = region.Size

	local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, ignore, math.huge)

	if #partsInRegion ~= 0 then
		for i,v in pairs(partsInRegion) do
			local humanoid = v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid")
			if humanoid then
				remoteF:FireClient(humanoid.Parent)
				print(humanoid.Parent)
				humanoid.HealthChanged:Connect(function(hp)
					humanoid.Health = 100
					wait(0.1)
				end)
			end

		end


	end

end

EDIT: You can use this function for getting all players in region if you struggle to do it:

local function getAllPlayersInRegion(regionParts)
	local tableOfPlayers = {}
	for i, v in pairs(regionParts) do
		local player = game:GetService("Players"):GetPlayerFromCharacter(v:FindFirstAncestorOfClass("Model"))
		if player then
			table.insert(tableOfPlayers, player)
		end
	end
	return tableOfPlayers
end
1 Like

How to delete the GUI if player left the region:
Use playersInRegion table to remove the GUI from players, who are outside the region. Use and modify this piece of code:

for i, v in pairs(game:GetService("Players"):GetPlayers()) do
	if table.find(playersInRegion, v) == nil and v.PlayerGui:FindFirstChild("GUI_NAME_HERE") then
		v.PLayerGui.GUI_NAME_HERE:Destroy()
	end
end
1 Like