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

My final version:

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(1)
		return
	end
	debounce = true
	debounce2 = false
	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(1)
		return
	end
	debounce = false
	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)

image

It worked fine for a bit, but when I decided to jump off at the corner of the platform, the number went down to -1, and when I stepped back on it went to 0. So, yeah.

It went to -1 when I step and when I came back to spawn it became 1, where did 0 go?

Just add a check to prevent the number from ever going below 0.

I’m not responsible for any of the BillboardGui elements.

I am showing the number printed when I stepped on brick, not to focus on error

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

Likely caused by the -1 issue, although that’ll occur because no check was in place to prevent the counter decreasing below 0.

I tried to play with 2 people using your script, but it only detected one.

Do you know why this happens? Try using the most recent version.

Also, in team test, why does the number goes to -1 ,-2

instead of 1 or 2

It won’t, the recently added check prevents the counter going below 0.

The line that says
“if plrtouch > 0 then”
is the check right?

1 Like

Yes, so it prevents the script negating 1 if the number is already 0.

Change the first one to “if plrtouch >= 0 then”. I’ve edited the entire script too.

My script works now, just added a debounce

local playerstuching = 0
local tableofplayers = {}
local canwork = true

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

script.Parent.TouchEnded:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and canwork == true then
        canwork = false
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if table.find(tableofplayers,player.Name) then
            table.insert(tableofplayers,player.Name)
            playerstuching -= 1
        end
        task.wait(0.5)
        canwork = true
    end
end)

while wait(1) do
    print(playerstuching)
end

I walked onto the platform, it turned to 1, then walked off, and then it turned to zero.
But it started going down when I did the same process again.

Your previous script work for me :sweat_smile:

Edit: I’m sorry your script only work for singular player and when part not cover all part of player character. For multiplayer, it completely doesn’t work. Sorry, my mistake for not testing correctly. :sweat_smile:

https://developer.roblox.com/en-us/api-reference/event/BasePart/TouchEnded

--put this script inside a part
--also make sure the part is CanCollide off, 
	--if not it won't work correctly as .TouchEnded event can fired instantly when player move a little bit
	--and surprisingly I realized if part CanCollide is set to false and if the player move inside it freely,
	-- .Touched event won't fired for the same part again unless the player spam jumping
local part = script.Parent

-- Create a BillboardGui
local bbgui = Instance.new("BillboardGui", part)
bbgui.Size = UDim2.new(0, 200, 0, 50)
bbgui.Adornee = part
bbgui.AlwaysOnTop = true
local tl = Instance.new("TextLabel", bbgui)
tl.Size = UDim2.new(1, 0, 1, 0)
tl.TextScaled = true
tl.BackgroundTransparency = 1

local numTouchingParts = 0
local players = {}
local function onTouch(part)
	print("Touch started: " .. part.Name)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player and players[player] then return end
	if player then
		players[player] = true
		numTouchingParts = numTouchingParts + 1
	end
	tl.Text = numTouchingParts
end

local function onTouchEnded(part)
	print("Touch ended: " .. part.Name)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player and players[player] then 
		players[player] = nil
		numTouchingParts = numTouchingParts - 1
	end
	tl.Text = numTouchingParts
end

part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)