Problems with map voting system

I’m trying to make voting pads, where a vote is counted as long as the player stands on the pad. The script doesn’t work though, so I’m probably doing something wrong. Would be great if I could get some tips on how to fix it :wink: Thanks in advance.

CurrentStatus = ‘Voting’

local Map1Votes = {}
local Map2Votes = {}
local Map3Votes = {}

Vote1.VotePad.Touched:connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not Player or not CurrentStatus == 'Voting' then return end
	for index, touch in pairs(Map1Votes) do
		if Player ~= touch then
			table.insert(Map1Votes, Player)
			Vote1.VotePad.GUI.Frame.Amount.Text = #Map1Votes
			print(unpack(Map1Votes))
		end
	end
end)

Vote1.VotePad.TouchEnded:connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not Player or not CurrentStatus == 'Voting' then return end
	for index, touch in pairs(Map1Votes) do
		if Player == touch then
			table.remove(Map1Votes, index)
			Vote1.VotePad.GUI.Frame.Amount.Text = #Map1Votes
			print(unpack(Map1Votes))
		end
	end
end)
1 Like

I do the same thing for map voting, but I use a Region3 instead of using Tocuhed/TouchEnded

3 Likes

So your system detects whenever or not the player is located within the Region3 positions?

Would probably work better than Touched/TouchEnded, but I can’t say I’m certain on how to use it :confused:

Just create a region3 for each pad:

local point1 = pad1.Position + Vector3.new(pad1.Size.X/2, 0, pad1.Size.Z/2)
local point2 = pad1.Position + Vector3.new(-(pad1.Size.X/2), 15, -(pad1.Size.Z/2))
local region = Region3.new(point1, point2)

while wait() do
for x, player in pairs(game.Players:GetPlayers()) do
-- check players are in the region --
-- continue logic etc. --
end
end

http://wiki.roblox.com/index.php?title=API:Class/Workspace/FindPartsInRegion3

edit: accidentally sent before finishing lol

1 Like

What is not working exactly?

1 Like

No idea, it doesn’t generate any error msgs. However, it kind of worked before I added this in the first paragraph:

for index, touch in pairs(Map1Votes) do
if Player ~= touch then

^^^ I added it as a debounce, because the engine added the player to the table several times

What you could do is index the players instead. You don’t need to worry about a debounce then: table[player] = true

Makes indexing easier too.

1 Like

Aight, I’ll try

Hmm doesn’t work :confused:
Btw, once the player stops moving, the pad doesn’t detect the vote anymore.

Maybe I should try Region3 after all.

The code functions like this

2 parts of the character begin touching the votespace. Character is added to the vote list.
1 part of the character stops touching the votespace. Character is removed from the vote list.

Even though the character is still in the votespace, when any part of them leaves it, they are counted as totally leaving it. With r15, lots of parts could be entering and leaving he votespace as the character walks onto/into it. And also parts could be leaving it frequently too. All the code does is keep track if the last event was a part entering or leaving.

1 Like

Easy to use modular system if you want :wink: https://www.youtube.com/watch?v=1eUCfvC7F1A

I guess if you wanted to be overly efficient, you could keep a table of all the players in the server’s HumanoidRootParts, and pass that as a whitelist to the region3 function.

1 Like

Can I just take a moment to say, you should be using " :Connect " instead of " :connect ". As the latter is deprecated.

3 Likes

You might want to do
if not Player or not (CurrentStatus == 'Voting') then return end
instead of
if not Player or not CurrentStatus == 'Voting' then return end

I’ve had problems arise from small things like that.
If this doesn’t fix it, could you explain your error or problem a little bit more?

1 Like

That doesn’t work either. I’ve found out what’s not working tho. It’s the lines highlighted in gray.

Vote1.VotePad.Touched:Connect(function(hit)
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not Player or not (CurrentStatus == 'Voting') then return end
		--[[for index, touch in pairs(Map1Votes) do
			if Player ~= touch then]]
				table.insert(Map1Votes, Player)
				Vote1.VotePad.GUI.Frame.Amount.Text = #Map1Votes
				print(unpack(Map1Votes))
			--end
		--end
	end)
1 Like