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)
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.
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)
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
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.
--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)