Else statement breaks the script

  1. What do you want to achieve? Hello, I’m making a script that prints the player name in the console when they are in the zone, and when they are not prints that a player is not in the zone

  2. What solutions have you tried so far? I have tried

local Workspace = game:GetService("Workspace")
local zone = script.Parent

while wait(.1) do
	local partsInZone = Workspace:GetPartsInPart(zone)
	for i,v in pairs(partsInZone) do
		if v.Parent:FindFirstChild("Humanoid") then
			print(v.Parent.Name, " Is in zone")
		end
	end
	print("The zone is empty")
end

But this seems to print “The zone is empty” even when a player is in the zone

Current code:

local Workspace = game:GetService("Workspace")
local zone = script.Parent

while wait(.1) do
	local partsInZone = Workspace:GetPartsInPart(zone)
	for i,v in pairs(partsInZone) do
		if v.Parent:FindFirstChild("Humanoid") then
			print(v.Parent.Name, " Is in zone")
		else
			print("The zone is empty")
		end
	end
end

Thanks!

1 Like
local zone = script.Parent

while wait(.1) do
	
	local partsInZone = workspace:GetPartsInPart(zone)	
	if #partsInZone<1 then print("The zone is empty")
	else
		for i,v in pairs(partsInZone) do
			if v.Parent:FindFirstChild("Humanoid") then print(v.Parent.Name, " Is in zone") end
		end
	end
	
end
1 Like

But what if I’m gonna have other parts in the zone? Maybe multiple players!

Soy you want to specifically only check for humanoids?

I’d use ZonePlus to do this. It already has built-in functions to get players in a zone if I recall.

Yes, for players! Because I intend on updating a script so that it shop a specific GUI when a player enter the zone.

The pairs loop will give you all the players in the zone

I would prefer having my own code. I understand that theres is nothing bad in using plugins

Yeah I understand that, but how to know if there aren’t any players in the zone then because that is my problem from the start.

local zone = script.Parent

while wait(.1) do

	local partsInZone = workspace:GetPartsInPart(zone)
	local humanoids=0
	for i,v in pairs(partsInZone) do
		if v.Parent:FindFirstChild("Humanoid") then
			print(v.Parent.Name, " Is in zone")
			humanoids+=1
		end
	end
	if humanoids<1 then print("The zone is empty") end
	
end

Well this would work but then how do I detect if a Player has left the zone?

To be honest it sounds to me like you could just use a ProximityPrompt to do this, When a player is close it will show the shop gui and when they leave the area the gui can be hidden.

Yeah but proximity prompts use BillBaordGuis, and I want to be using screen gui!
So I can display a gui when a player enters and hide it when they leave

ProximityPrompts can use whatever type of gui you want. Just change their style to Custom in the properties window.

I’ll give it a try and update you later!

Here’s a quick example for you:
Create a ProximityPrompt inside a part in workspace and name it “MyStorePrompt”, change it’s style to Custom in it’s properties
Create a GUI in PlayerGui for your store and add a localscript to it. Paste the following code into your LocalScript:

local MyStoreGui=script.Parent
local PPS=game:GetService("ProximityPromptService")
PPS.PromptShown:Connect(function(prompt) if prompt.Name=="MyStorePrompt" then MyStoreGui.Enabled=true end end)
PPS.PromptHidden:Connect(function(prompt) if prompt.Name=="MyStorePrompt" then MyStoreGui.Enabled=false end end)
1 Like

Ohh this seems simpler than I thought! Thanks!

1 Like

Check if this code works.

local zone = script.Parent

while wait(.1) do
	local partsInZone = workspace:GetPartsInPart(zone)
	local playersInZone = {}
	
	for index, v in pairs(partsInZone) do
		if v.Parent:FindFirstChild("Humanoid") then
			table.insert(playersInZone, v.Parent.Name)
		end
	end
	
	if #playersInZone == 0 then
            print("There's no player in the zone")
        end
end

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