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

How do I make a pad that counts how many players are on it at a given time?

Asked by 6 years ago
Edited 6 years ago

I am wanting a brick that counts how many players are standing on it, like in map voting. This is what I have. I have no idea why this doesn't work. When I stand on it, it prints 2 instead of 1. Anyone know the problem? Thank you.

--[[_G.idiots = 0

function Touched(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        if game.Players:playerFromCharacter(hit.Parent).TeamColor==game.Teams:findFirstChild("foreigner").TeamColor then

        end
        _G.idiots = _G.idiots + 1   
        print(_G.idiots)
    end
end

script.Parent.Touched:Connect(Touched)]]--

local Player
local Part = workspace.pad.Part
local PlayersTouching = {} --Creating a table. You and I are currently touching the part.

game.Players.PlayerAdded:Connect(function(Plr)
    Player = Plr
end)

Part.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        table.insert(PlayersTouching, Hit.Parent.Name) --Putting the name of the player in the table
    end
end)

Part.TouchEnded:Connect(function(Hit) --If they stop touching the part
    if Hit.Parent:FindFirstChild("Humanoid") then
        for i,v in pairs(PlayersTouching) do
            if v == Player.Name then
                PlayersTouching[i] = nil --Removing the player from the table
            end
        end
    end
end)

while true do
    print(#PlayersTouching)
    wait(1)
end

0
Wow, was that map a bad map? Was that how the variable name was created? hiimgoodpack 2009 — 6y
0
They are called idiots because the people using the pad always act like idiots JarFullOfMayonnaise 48 — 6y
0
Use table.remove on line 34 R_alatch 394 — 6y
0
Im going to stick with, PlayersTouching[i] = nil. Table.remove didn't work JarFullOfMayonnaise 48 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You did not define idiots.

local idiots = 0
script.Parent.Touched:Connect(function(hit) --:connect is deprecated, use :Connect
    if game.Players:GetPlayerFromCharacter(hit.Parent) then --best way to get the player
        if plr.Character:FindFirstChildOfClass("Humanoid") then
            idiots = idiots +1
    end
end
end)
while wait(1) do --use while wait(1) instead of while true do and adding a wait
    print(idiots)
end

Please accept my answer if this helped!

0
didn't work JarFullOfMayonnaise 48 — 6y
0
if :GetPlayerFromCharacter() returns nil (because the wasn't a character to begin with) then the line after it won't work because you're trying to index nil (because plr is nil). XAXA 1569 — 6y
0
Even when I step on it, still nothing JarFullOfMayonnaise 48 — 6y
0
Use your debugging skills, even if nothing happened, you should have seen an error in the output. plr is not defined. PreciseLogic 271 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

I think this should do it, hope i helped!

_G.idiots = 0

function Touched(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        _G.idiots = _G.idiots + 1
        print(_G.idiots)
    end
end

script.Parent.Touched:Connect(Touched)




0
Explain your answer in English. Don't just post code; this doesn't help the poster or the community understand why the code does what it does. hiimgoodpack 2009 — 6y
0
Sorry, but that didn't work. JarFullOfMayonnaise 48 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Using a table and a generic for loop, you can insert the players that touches the BasePart.

local Player
local Part = workspace.Part
local PlayersTouching = {"SimplyRekt", "BoxedHouses"} --Creating a table. You and I are currently touching the part.

game.Players.PlayerAdded:Connect(function(Plr)
    Player = Plr
end)

Part.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        table.insert(PlayersTouching, Hit.Parent.Name) --Putting the name of the player in the table
    end
end)

Part.TouchEnded:Connect(function(Hit) --If they stop touching the part
    if Hit.Parent:FindFirstChild("Humanoid") then
        for i,v in pairs(PlayersTouching) do
            if v == Player.Name then
                PlayersTouching[i] = nil --Removing the player from the table
            end
        end
    end
end)

print(#PlayersTouching)
0
29: if v == Player.Name then Workspace.pad.Part.Script:29: attempt to index upvalue 'Player' (a nil value) JarFullOfMayonnaise 48 — 6y

Answer this question