Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to count numbers on a part in a text label?

Asked by 3 years ago

i have this script: THIS SCRIPT IS IN A TEXT LABEL, WICH IS IN A SURFACE GUI, WICH IS IN THE ACTUAL PART

local part = script.Parent.Parent.Parent
local numTouchingParts = 0 
local playersTouching = {}
part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --checks to make sure it is a player
        local c = hit.Parent
        if not playersTouching[c] then --if the player is not already touching the part, add them to the table and increment numTouchingParts by one
            table.insert(playersTouching, c)
            numTouchingParts = numTouchingParts + 1
        end
    end
end)

part.TouchedEnded:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local c = hit.Parent
        if playersTouching[c] then -- if the player is touching the part then remove them from the table and decrement numTouchingParts by 1
            playersTouching[c] = nil
            numTouchingParts = numTouchingParts - 1
        end
    end
end) -- here the player counter script finishes
script.Parent.Text = tostring(numTouchingParts.Value) -- here i tried to make the text label show how many players are on the part

but nothing happens.. can i get some help?

1 answer

Log in to vote
2
Answered by 3 years ago
local Players=game:GetService"Players"
local function getPlayersOnPart(regionPart)
local pos1 = regionPart.Position+Vector3.new(0,.3,0) - (regionPart.Size / 2)
local pos2 = regionPart.Position+Vector3.new(0,.3,0) + (regionPart.Size / 2)
local region = Region3.new(pos1, pos2)
local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
local tab,x={},0
for _, v in ipairs(partsInRegion) do
        local char = v.Parent
        local hum = char and char:FindFirstChildOfClass"Humanoid"
        local player=hum and Players:GetPlayerFromCharacter(char)
    if player and not tab[player]then
            tab[player]=true
            x+=1
    end
    end
    for _,v in ipairs(Players:GetPlayers())do
        tab[v]=not not tab[v]
    end
return tab,x
end


local textLabel=script.Parent
local part,debounce=textLabel.Parent.Parent,false
part.Touched:Connect(function()
if debounce then return end;debounce=true
local _,x=getPlayersOnPart(part)
textLabel.Text=tostring(x)
wait(1)
debounce=false;
end)
while wait(4)do
local _,x=getPlayersOnPart(part)
textLabel.Text=tostring(x)
end



Ad

Answer this question