How to detect how many players are touching a part


image
I want to make a script that detect how many players are touching the part (Green Circle)
so if 1 player is touching the text changes to 1/2 and if 2 players are touching it the text changes to 2/2 so when 1 player stand on it the text changes to 1/2 and if 2 players and standing on it the text changes to 2/2 so while 1 player is standing on the green circle the text change to 1/2 and while 2 player are standing on the green circle the text changes to 2/2 and while 0 players are standing on the green circle the text changes to 0/2

I made this script but it is not working cause the PlayersTouching is being negative cause the player touch the green circle more than 1 time (loop)

local SpotScreen = script.Parent
local ScreenText = SpotScreen.SurfaceGui.ScreenFrame.ScreenText
local PlayersTouching = 0
SpotScreen.Touched:Connect(function(Touch)
	if Touch.Parent:IsA("Model") then
		PlayersTouching = PlayersTouching +1
		ScreenText.Text = PlayersTouching .. "/2"
		
		SpotScreen.TouchEnded:Connect(function(TouchEnd)
			PlayersTouching = PlayersTouching -1
			ScreenText.Text = PlayersTouching .. "/2"
		end)
	end
end)
2 Likes

You can try to add a stringValue in a folder callled “players” when a player touch the part, the string would contain the userID of the player. So when he touch the part again (loop) you can check if he was added before in a string value.

For example if two players touches the part, you will have two stringValue containing their userID.
With that you can count how many stringValues have been added and that will be the number you want.

If it is not clear tell me i will try to explain better

1 Like

You can keep most of your code but when a player steps on the pad, in a table check for their name, and if their name is not their, add their name. if they step off, :TouchEnded , then remove them from the table. Everytime someone is added to the table check the number of the amount of players in the table#tablename

I hope this helps, if you have any further questions let me know!

1 Like

try rearranging your code

local SpotScreen = script.Parent
local ScreenText = SpotScreen.SurfaceGui.ScreenFrame.ScreenText
local PlayersTouching = 0
SpotScreen.Touched:Connect(function(Touch)
	if Touch.Parent:IsA("Model") then
		PlayersTouching += 1
		ScreenText.Text = PlayersTouching .. "/2"
	end
end)
SpotScreen.TouchEnded:Connect(function(TouchEnd)
	if Touch.Parent:IsA('Model') then
		PlayersTouching -= 1
		ScreenText.Text = PlayersTouching .. "/2"
	end
end)
1 Like

how to check if he was added before in a string value ?

You can search the object, for example:
local object = game.workspace:FindFirstChild(userID)

But try the solution of @shipmaster2410 , it is cleaner.
The idea is the same but instead of creating stringValue for each players you add him in a table.
If the player touches the part again (loop) check if he his already in that table, if yes don’t add +1 to the player counter.

1 Like

but how to make a table :sweat_smile: I did not make one before

but how to make a table :sweat_smile: I did not make one before>

There are also libraries that help with this

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