Map Voting Script not working properly

So I made a map voting script for my project and It doesn’t seem work properly. What it is supposed to do is that it checks if a player has already voted by checking the value of a string value that is placed inside the player(not Character) and if it does meet the if statement, it will add + 1 vote or if it doesn’t meet the if statement, it will subtract the (previous voting pad’s value to -1 ) and do nothing.

I’ve tried debugging using print statements, rechecking for any errors to no avail.

Note: It works if I use a while loop for calling those functions somehow.

local Players = game:GetService("Players")

local VotingSystem  = script.Parent

local Pad1 = VotingSystem.Pad1
local Pad2 = VotingSystem.Pad2
local Pad3 = VotingSystem.Pad3
-- hi
local VoteScreen1 = VotingSystem.VoteScreen1
local VoteScreen2 = VotingSystem.VoteScreen2
local VoteScreen3 = VotingSystem.VoteScreen3

local function updateVoteScreen1Display()
	VoteScreen1.Votes.SurfaceGui.Text.Text = tostring(Pad1.Votes.Value)
end

local function updateVoteScreen2Display()
	VoteScreen2.Votes.SurfaceGui.Text.Text = tostring(Pad2.Votes.Value)
end

local function updateVoteScreen3Display()
	VoteScreen3.Votes.SurfaceGui.Text.Text = tostring(Pad3.Votes.Value)
end

Pad1.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player then								
		if Player.Voted.Value ~= Pad1.Name then		
		    Pad1.Votes.Value = Pad1.Votes.Value + 1	

			if Player.Voted.Value ~= "N/A" then
			    local PreviousPad = VotingSystem:FindFirstChild(Player.Voted.Value) 
				PreviousPad.Votes.Value = PreviousPad.Votes.Value - 1
				updateVoteScreen1Display()
			end
			Player.Voted.Value = Pad1.Name
			updateVoteScreen1Display()
		end
	end
end)

Pad2.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		if Player.Voted.Value ~= Pad2.Name then
			Pad2.Votes.Value = Pad2.Votes.Value + 1
			Player.Voted.Value = Pad2.Name
		if Player.Voted.Value ~= "N/A" then
			    local PreviousPad = VotingSystem:FindFirstChild(Player.Voted.Value) 
				PreviousPad.Votes.Value = PreviousPad.Votes.Value - 1
			end
			 Player.Voted.Value = Pad2.Name
			 updateVoteScreen2Display()
		end
	end
end)

Pad3.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		if Player.Voted.Value ~= Pad3.Name then
		    Pad3.Votes.Value = Pad3.Votes.Value + 1
			
		if Player.Voted.Value ~= "N/A" then
			    local PreviousPad = VotingSystem:FindFirstChild(Player.Voted.Value) 
				PreviousPad.Votes.Value = PreviousPad.Votes.Value - 1
			end
			Player.Voted.Value = Pad3.Name
			updateVoteScreen3Display()
		end
	end
end)
2 Likes