I’d like to be able to check if all players in the server are touching a certain part, how would I do that?
It’s normal.
Just create part at workspace. add script at the part!
here’s the code.
local Players = game:GetService("Players") -- Getting the service.
local Part = script.Parent -- Getting the part to the script.
Part.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(Part.Parent) -- Getting who touched the part.
print(player.Name .. " Has touched the part.!") -- You can remove that if you want too. that will print who touched the part. at Output!
-- do your stuff here
end)
no they want to know if everyone is on the part at once
not just one individual player
Do you mean.
Get their names??
no I mean they want to check if every player in the game is touching a certain part
I said nothing about names
This is quite easy to do.
local part = script.Parent
local plrs = game.Players:GetPlayers -- we get the table of players
local numTouchingParts = 0
local function onTouch(part) --we make a function to add 1 if a player touches
numTouchingParts += 1
end
local function onTouchEnded(part) --we make a function to remove 1 if a player leaves
numTouchingParts -= 1
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
if numTouchingParts == #plrs then --we see if the length of the table (players in the server) is equal to the value in numTouchingParts
--do ur magic
end
This would add a number for both feet of the player (or any other part which is touching it that isn’t even a player). Meaning one player is counted as two.
When testing the numTouchingParts is equal to every body part that is touching the part.
Oh yeah, i forgot to add in an IsA() check
local touchingPlayers = {}
function startGame()
--all players in game touching part
end
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
local alredyInArray = table.find(touchingPlayers,player)
if alredyInArray then return end
table.insert(touchingPlayers,player)
if #touchingPlayers = #game.Players:GetPlayers() then
startGame()
end
end
end)
part.TouchEnded:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
local index = table.find(touchingPlayers ,player)
if index then
table.remove(touchingPlayers,index)
end
end
end)
This doesn’t seem to work, even though I am touching the part it’s still printing the else
he did not check if the player alredy exists in the array
I have a wee error code for line 25 on this one.
“Workspace.Part.Script:25: invalid argument #2 to ‘remove’ (number expected, got Instance)”
replace table.remove(touchingPlayers,player)
with table.remove(touchingPlayers,index)
i made an edit, everything will work fine now…
That’s perfect! Thank you very much
no problem, try to read each line and understand it.
doesn’t roblox have a new api and isn’t Region3 Deprecated?
Are you sure? It worked on my end when testing.
If the part has collisions enabled then make the Region3 3 or 4 studs larger on the Y-axis. Since I tested with a collisions disabled part.
local part = workspace:WaitForChild("Part")
local players = game:GetService("Players")
local regionThree = Region3.new(part.Position - part.Size/2, part.Position + part.Size/2+Vector3.new(0,5,0))
while task.wait(3) do
local playerList = players:GetPlayers()
local playersInRegion = {}
local regionParts = workspace:FindPartsInRegion3(regionThree, part)
for i, v in pairs(regionParts) do
print(v)
if v.Parent:FindFirstChild("HumanoidRootPart") and v.Name == "Head" then
local plr = players:GetPlayerFromCharacter(v.Parent)
table.insert(playersInRegion, plr)
end
end
print(#playerList, #playersInRegion)
if #playerList == #playersInRegion then
print("Every player is touching the part!")
else
print("The part is not being touched by every player!")
end
end
Region3 is the way to go.
What are you talking about? That wasn’t the issue whatsoever. It was due to the Region3 being of the part, but if the part has collisions enabled then the player won’t enter that Region3 so the Region3 needs to be slightly increased in size.