Help needed with a function

Hello, dear forum members! I am currently scripting one of my games and currently have a question. I actually want to create a function that detects how many players are stepping on a specified part and then print their usernames.

Example: If three players called HyperD3v, Crytuls and xHappy_z are standing on the part when the function is being fired by the server, I would like the console to print “HyperD3v, Crytuls and xHappy_z are standing on the part!”.

Thanks for future help!
~ Arlenox.

I will try to think of something but i think its imposible for 3 or 2…

Sorry for broken English…

That’s actualy possible (example: Epic minigames).

You would add the players name into a table when the touch it, and when they stop touching it remove the player from the table.

By using:

tabel.insert
--and
tabel.remove


This is best that i cloud get thinking of:
player1=false
player2=false
player3=false
script.Parent.Touched:Connect(function(obj)
if player1==true and player2==true and player3==true then
print(“HyperD3v, Crytuls and xHappy_z are standing on the part!”)
end
if obj.Parent:findFirstChild(“Humanoid”)~=nil then
p=game.Players:findFirstChild(obj.Parent.Name)
if p~=nil then
if p.Name==“HyperD3v” then
player1=true
end
if p.Name==“Crytuls” then
player2=true
end
if p.Name==“xHappy_z” then
player3=true
end
end
end
end)

Note:this script should be disabled and enabled every 2-3 sec with other script

script.Parent.Script.Disabled=true
script.Parent.Script.Disabled=false
wait(2)

Not best one, but im now sick, i cant think of anything difrent…

That’s a not a good idea, OP was just using their names as an example, i suggest using tables.

How to create a table and add players in it? I never used it before and I can’t understand how it works. :confused:

Part has a method called :GetTouchingParts() which You can use to detect if another part that touches this part is parented to someone’s character, but first You need to add this line into script

script.Parent.Touched:connect(function() end)) --Assumed the script is parented into a detector part

to create a TouchInterest object, otherwise this metod will return a empty table.

Tables are very useful, you should use them.

I made this quick example, here is the script:

--This probably isn't the best way to do it, but i'm not that good at scipting.
local TouchPart = workspace.TouchPart--Part the player needs to touch.
local Button = workspace.Button.ClickDetector--Button that will print the players names when pressed.

local PlayersTouchingPart = {}--Creating a table.

local Debounce = false--DEBOUNCE

TouchPart.Touched:Connect(function(hit)
	if Debounce == false then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)--Getting the player
		if Player then-- If it is a player \/ will run.
			if not table.find(PlayersTouchingPart, Player.Name) then--Checks to see if the player is already in the table and if they are not then below will run.
				table.insert(PlayersTouchingPart, Player.Name)--using table.insert
			end
		end
		wait(1)
		Debounce = true
	end
end)

TouchPart.TouchEnded:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player and Debounce then
		table.remove(PlayersTouchingPart, table.find(PlayersTouchingPart, Player.Name))--The way table.remove works is you need to specify the index you want to remove, but we don't know that, so we use table.find which gives us the index according to the value.
		wait(1)
		Debounce = false
	end
end)

Button.MouseClick:Connect(function()
	for index, playersintabel in pairs(PlayersTouchingPart) do -- for i, v in pairs loop(runs for every value inside of a table, folder etc)
		print(playersintabel)--prints the value.
	end
end)

Sorry, I am not the best at explaining.

you can easily do that with :GetTouchingParts()
here is a quick function i wrote with an example that prints the players touching a part each 1 second

local plrs = game:GetService("Players")

local function getPlayersTouchingPart(part)
	local touchtransmitter
	touchtransmitter = part.Touched:Connect(function()
		
	end)
	local pizza = part:GetTouchingParts()
	local is = {}
	for delicious, v in pairs(pizza) do
		local plr = plrs:GetPlayerFromCharacter(v.Parent) or plrs:GetPlayerFromCharacter(v.Parent.Parent)
		if plr then
			if not is[plr] then
				is[plr] = true
			end
		end
	end
	touchtransmitter:Disconnect()
	return is
end

while wait(1) do
	local plrs = getPlayersTouchingPart(workspace.Part)
	local plrsname = {}
	for i, v in pairs(plrs) do
		table.insert(plrsname,i.Name)
	end
	print(table.concat(plrsname,", ").. " are touching the part!")
end