Trying to make block show how many players are standing on it

So basically, I want to make a GUI show how many players are standing on a block. So I decided to give it a try, but it didn’t turn out to work that well.
robloxapp-20211024-1318058.wmv (1.4 MB)
As you can see in the video, when I step on the block, the number goes up to 18, even though I am only one player. When I step back off, it goes back to zero. And the same thing happens over and over. I will admit, it looks pretty cool, but not what I am trying to achieve.

Here is my code:

local hello = script.Parent
plrtouch = 0
hello.Touched:connect(function()
	plrtouch = plrtouch + 1
	script.Parent.BillboardGui.TextLabel.Text = plrtouch
end)
hello.TouchEnded:connect(function()
	plrtouch = plrtouch - 1
	script.Parent.BillboardGui.TextLabel.Text = plrtouch
end)

Any help is appreciated!
(Alright this is solved! Thanks for everyone who helped me out, now I can finally begin working on my player queue😅)

You should probably check to make sure it’s not detecting multiple parts in one player’s character.

There is probably more efficient ways of doing what you want but, I recommend creating a BoolValue inside the player and when the player is touching it, it’ll set the BoolValue to true, that will prevent players from touching it multiple times.

local hello = script.Parent
plrtouch = 0
hello.Touched:connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player touching the part
		plrtouch = plrtouch + 1
		script.Parent.BillboardGui.TextLabel.Text = plrtouch
	end
end)
hello.TouchEnded:connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player no longer touching the part
		plrtouch = plrtouch - 1
		script.Parent.BillboardGui.TextLabel.Text = plrtouch
	end
end)

You just needed to add a check to see that it is a player touching the part, since non-player parts can cause the Touched event to fire.

Its pretty simple, you made a script which will add the number of parts hit

Your legs,hands and all other body stuff, counts as 18

Try this script below:

local playerstuching = 0
local tableofplayers = {}

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if not table.find(tableofplayers,player.Name) then
            table.insert(tableofplayers,player.Name)
            playerstuching += 1
        end 
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if table.find(tableofplayers,player.Name) then
            table.insert(tableofplayers,player.Name)
            playerstuching += 1
        end 
    end
end)

Same player might touch the brick twice, thats not too valid for his question i guess?

For a touched event to be fired twice between the same parts a touchended event must be fired in between.

Thanks for the response! But it didn’t work. Whenever I stepped on my block it went up to 16, instead of 18. So that’s a change I suppose.

It’ll work if you add a debounce.

local hello = script.Parent
local debounce = false
plrtouch = 0
hello.Touched:connect(function(otherPart)
	if debounce then
		debounce = false
		task.wait(5)
		return
	end
	debounce = true
	if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player touching the part
		plrtouch = plrtouch + 1
		script.Parent.BillboardGui.TextLabel.Text = plrtouch
	end
end)
hello.TouchEnded:connect(function(otherPart)
	if debounce then
		debounce = false
		task.wait(5)
		return
	end
	debounce = true
	if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player no longer touching the part
		plrtouch = plrtouch - 1
		script.Parent.BillboardGui.TextLabel.Text = plrtouch
	end
end)

More info:

You can change task.wait(5) to a shorter length of time, this will restrict the amount of times a player can trigger both events.

its not about debounce, its about the player
He needs the number of players, and if you run this, if player hits the brick twice, it will count as 2

We need to check if player was already added and then change number of count

If the player hits the brick twice that must mean that in between they stopped hitting the brick, hence the touchended event would fire and the counter would be reduced by 1. The debounce prevents the functions connected to both of the events from being executed too frequently.

See, now I touch brick
Number = 1
5-10 seconds later debounce becomes false and script works again

I walk on brick without jumping so it won’t count as player left the brick
Number = 2

I tested that, and this is result

local hello = script.Parent
local debounce = false
local debounce2 = true
plrtouch = 0
hello.Touched:connect(function(otherPart)
	if debounce then
		debounce = true
		debounce2 = false
		task.wait(5)
		return
	end
	debounce = true
	if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player touching the part
		plrtouch = plrtouch + 1
		script.Parent.BillboardGui.TextLabel.Text = plrtouch
	end
end)
hello.TouchEnded:connect(function(otherPart)
	if debounce2 then
		debounce2 = true
		debounce = false
		task.wait(5)
		return
	end
	debounce2 = true
	if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player no longer touching the part
		plrtouch = plrtouch - 1
		script.Parent.BillboardGui.TextLabel.Text = plrtouch
	end
end)

A double debounce fixes that.

It does not, debounce is a cool down, remember that

If player touches the brick after long time, it counted as 2

Check the logic inside the code, although I agree a players array is best suited for this use case. In the above a player can’t trigger the touched event twice in a row before triggering a touchended event, similarly this works the other way around too. Again “task.wait(5)” should be changed to a shorter time length.

I am not checking code, I tested it in studio, the number says 2 not 1? why?

Oh, there was a slight error, had to change “debounce = true” to “debounce2 = true” in the touchended event.

I used your script, with a few modifications for it to show on the billboard gui

local playerstuching = 0
local tableofplayers = {}

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not table.find(tableofplayers,player.Name) then
table.insert(tableofplayers,player.Name)
playerstuching += 1
script.Parent.BillboardGui.TextLabel.Text = playerstuching
end
end
end)

script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if table.find(tableofplayers,player.Name) then
table.insert(tableofplayers,player.Name)
playerstuching += 1
script.Parent.BillboardGui.TextLabel.Text = playerstuching
end
end
end)

And this is what I got:
robloxapp-20211024-1408139.wmv (1.9 MB)

I don’t know why it is doing this.

No, its totally gone wrong

Also why are you obsessed with many debounces

Check it in roblox studio, it did not work for me

wat

wow it worked for me, but it says 45-47 for u?

lemme make the script again