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