The best way to go about making voting pads?

Hey, so I know how to create this, but there’s quite a few different methods I could use, and am not sure what would be optimal.

I could use raycasting to see if the player is underneath a pad, then send in a remote event to update whether or not the player is on that pad.

I know Region3’s are an option as well, which could be reliable to get any player in range of the pad.

And there’s plenty others as well, I was maybe thinking about TouchEnded and Touched events server-sided to update how many players are on either pad, I might be leaning more towards trying this one out.

Anywho, let me know what method might be the most efficient or optimal to use here. Thanks!

The easiest way by far would most likely be .Touched/.TouchEnded but for efficiency or optimally I wouldn’t exactly know.
Raycasting would most likely be to expensive since you would have to do it every second/move.
Region3 (I think) doesn’t have collision detection its just a DataType with size and position that could just be done internally anyways. Collision detection manually is generally very expensive so .Touched would be the best in the case.
Some less buggy alternatives might be:

  • GUI with buttons
  • Click Detectors
1 Like

The way I do this is:

  1. Wait for the pad to be touched using .Touched
  2. When the pad has been touched, check whether the player is actually on the pad using a Region3 check
  3. If the Region3 check passed, then activate the pad and keep doing the same Region3 check (I would suggest every Heartbeat) until the player is no longer on the pad
  4. Deactivate the pad

This is more flexible than using the .Touched/.TouchEnded combination as I’ve found .TouchEnded to be unreliable and by using this you can allow the player to jump on the pad after stepping on it.

2 Likes

So, while you could definitely use the methods you listed, I would recommend using a simpler method, such as simply detecting if a point is in a region. For example, the below code detects when a player is in a area (defined by a part), which you can use to detect if a player wants to vote for a certain pad. This method does not require casting rays or using region3s, it just uses math. In reality, all of the methods you listed would work well, and it really is up to you.

local function posInPos(pos,low,high)
	--A really long condition making sure that pos is between low and high
	return pos.X >= low.X and pos.X<=high.X and pos.Y >= low.Y and pos.Y<=high.Y and pos.Z >= low.Z and pos.Z<=high.Z
end

local function getPlayersInPart(part)
	local results = {}
	for _,plr in pairs(game:GetService("Players"):GetPlayers()) do
		--For every player, get their character
		local char = plr.Character
		if char then
			local humRoot = char:FindFirstChild("HumanoidRootPart")
			if humRoot then
				--Check to see if that character's humanoidRootPart is inside of the part
				local pos = humRoot.Position
				local low = part.Position - (part.Size/2)
				local high = part.Position + (part.Size/2)
				if posInPos(pos, low, high) then
					--If so, add it to the results
					table.insert(results, plr)
				end
			end
		end
	end
	--Return the results
	return results
end

local votes = {0, 0, 0} --Votes value, can use this to check stuff
local padLoc = workspace:WaitForChild("Voting areas") --Location areas are stored in

while true do --Update as often as you wish
	for _, part in pairs(padLoc:GetChildren()) do --Check each area
		local r = getPlayersInPart(part) --Table of players in part
		local num = tonumber(part.Name) --Number of the pad
		votes[num] = #r --Set the proper value to the total number in that area
		print(votes[num].." vote(s) for area "..num) --Print the result for this pad, just a example on how to use these values
	end
	wait(0.1)
end

Downloadable repro: regionCheckingVoting.rbxl (16.3 KB)

Side Note: .TouchEnded is not very reliable and I would recommend using another method instead

6 Likes

Thanks guys, really appreciate the info. :slightly_smiling_face:

1 Like