if more than ten players touch part then print("Hello") - how to make that ? it's local script or script ? part = game.Workspace.Part -Thank u and best regards -PPJASK
✓ ANSWER ACCEPTED
if more than ten players touch part then print("Hello") ?
2 answers
✓ Accepted answer
local touchingPlayers = 0 --make a local variable of the number of touching players
local function checkPrint() --make a local function that checks number of touching players
if touchingPlayers > 10 then
print("hello")
end
end
part.Touched:Connect(function(touchedPart)
local player = game.Players:GetPlayerFromCharacter(touchedPart.Parent) --if touched part is child of player's character then this will return player. if not it will return nil
if player then
touchingParts = touchingPlayers + 1 --if player then add 1 to number of touching players
checkPrint() --fire checkPrint function to see if there are 10 players touching the part
end
end)
part.TouchEnded:Connect(function(touchedPart)
local player = game.Players:GetPlayerFromCharacter(touchedPart)
if player then
touchingPlayers = touchingPlayers - 1 --if a player has stopped touching the part, subtract 1 from number of touching players
checkPrint()
end
end)
Let me know if this works
I recommend storing each player name that has touched the part into a table and then checking if the table has a length of more than 10. Doing it like this, you can prevent the same player from being added to the table and incrementing the number due to multiple interactions with the part.
Example (Server Script)
local TouchedPlayers = {}
script.Parent.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player then
if not table.find(TouchedPlayers, Player.Name) then
table.insert(TouchedPlayers, Player.Name)
if #TouchedPlayers > 10 then
print("Hello")
table.clear(TouchedPlayers)
end
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.
