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

How can I make a Script that shows how many people are standing on a brick?

Like a gui from Murder Mystery on the map voting.

5 answers

We would do it by setting a value for how many players have touched it. If a player touches it, its value goes up by one, thus telling how many players have touched it (if you wanted it to)

--[[Lets say the bricks name is Hello. we are going to set a variable for hello.]]--
hello = Workspace.Hello
plrtouch = 0
hello.Touched:connect(function()
plrtouch = plrtouch + 1
print(plrtouch.. " Players have touched this brick.")
end)

2 comments

0

but how can I make it so when they get off it subtracts that number?

0

You would have to use the TouchEnded function. Try looking it up.

Use BasePart.Touched to detect when a character steps on the brick:

local players={}
brick.Touched:connect(function(hit)
	local player=game.Players:playerFromCharacter(hit.Parent)
	players[player.Name]=true
end)

and use BasePart.TouchEnded to detect when the character steps off:

brick.TouchEnded:connect(function(hit)
	local player=game.Players:playerFromCharacter(hit.Parent)
	players[player.Name]=false
end)

2 comments

0

It's game.Players:GetPlayerFromCharacter, by the way.

0

deprecated method is shorter; stuck with it since it was the first way I saw it used

This is a little complex, but simple once you understand it. And as always, there are multiple ways to do it. I will give you the simplest one.

This is just the idea (no guarantee it will work):

local touching = {} -- Players that are touching
local part = script.Parent

part.Touched:connect(function(hit)
	local player = game.Players:playerFromCharacter(hit.Parent) 
	if not player then return end

	table.insert(touching, player)
end)

part.TouchEnded:connect(function(hit)
	local player = game.Players:playerFromCharacter(hit.Parent) 
	if not player then return end
	
	for index, touch in pairs(touching) do
		if player == touch then
			table.remove(touching, index)
		end
	end
end)

The code also stores each player, so maybe you can target a certain player or have each player be teleported at a certain time or something. Just an idea. To get the number, just do #touching.

The other way is much different. The idea is that it searches through all the players and checks the player's distance to the brick. This isn't as accurate as Touch and TouchEnded events.

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