Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

4 comments

0

Wow, was that map a bad map? Was that how the variable name was created?

0

They are called idiots because the people using the pad always act like idiots

0

Use table.remove on line 34

0

Im going to stick with, PlayersTouching[i] = nil. Table.remove didn't work

3 answers

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!

4 comments

0

didn't work

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

0

Even when I step on it, still nothing

0

Use your debugging skills, even if nothing happened, you should have seen an error in the output. plr is not defined.

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)




2 comments

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.

0

Sorry, but that didn't work.

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)

1 comment

0

29: if v == Player.Name then Workspace.pad.Part.Script:29: attempt to index upvalue 'Player' (a nil value)

This discussion is preserved as it was. New answers, comments, and votes are closed.