Voting system adds a value of 2+ instead of just 1

when the player steps on a part, i want the intvalue to add 1 to the value yk. but its adding more than 1. how would u prevent it from adding more than 1?

script:

local manfacevote = workspace.manfacevote
local catmanfacevote = workspace.catmanfacevote
local shaggymanfacevote = workspace.shaggymanfacevote

local votingvalues = game.ReplicatedStorage:WaitForChild("votingValues")

local plrName = {}

--parts
local voteParts = {
["manpart"] = workspace.manface,
["catpart"] = workspace.catmanface,
["shaggypart"] = workspace.shaggymanface
}


local vals = 0
local db = false

for partname,part in pairs(voteParts) do
	part.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if table.find(plrName, plr.Name) then return end
		if db then return end
		
		if partname == "manpart" then

			db=true
			vals = vals + 1
			if hit.Parent:FindFirstChild("Humanoid") then
				manfacevote.SurfaceGui.TextLabel.Text = "votes: "..vals
				votingvalues.manValue.Value = vals+1

				table.insert(plrName, plr.Name)
				print(plr.Name.." has voted. they can no longer vote again!")
			end
			db = false

This is because you are adding an extra vote yourself, votingvalues.manValue.Value = vals+1

You would need to just use the += operator to add, like this, votingvalues.manValue.Value += vals. Also instead of storing an extra vals value, you can directly operate on the Value object. This script will also not work for multiple voting parts like you have made as you are storing the vals value outside of the voting parts, so it is a global value. You are also checking the part name inside the for loop where you get each part, which is unnecessary, if you provide the entire code I can give you a better-structured version.

2 Likes

wow wow wow. absolutely perfect tysm. i tried doing manvalue.value = vals and i tried your way w/ manvalue.value += vals. either way works but whats the point of the +=?

i 100% agree w u. the script is messy but it got the job done but im open to any help! tysm :smiley:

script:

local manfacevote = workspace.manfacevote
local catmanfacevote = workspace.catmanfacevote
local shaggymanfacevote = workspace.shaggymanfacevote

local votingvalues = game.ReplicatedStorage:WaitForChild("votingValues")

local plrName = {}

--parts
local voteParts = {
["manpart"] = workspace.manface,
["catpart"] = workspace.catmanface,
["shaggypart"] = workspace.shaggymanface
}


local vals = 0
local db = false

for partname,part in pairs(voteParts) do
	part.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if table.find(plrName, plr.Name) then return end
		if db then return end
		
		if partname == "manpart" then

			db=true
			vals = vals + 1
			if hit.Parent:FindFirstChild("Humanoid") then
				manfacevote.SurfaceGui.TextLabel.Text = "votes: "..vals
				votingvalues.manValue.Value = vals

				table.insert(plrName, plr.Name)
				print(plr.Name.." has voted. they can no longer vote again!")
			end
			db = false
		
		
		
		elseif partname == "catpart" then

			db=true
			vals = vals + 1
			if hit.Parent:FindFirstChild("Humanoid") then
				catmanfacevote.SurfaceGui.TextLabel.Text = "votes: "..vals
				votingvalues.catValue.Value = vals

				table.insert(plrName, plr.Name)
				print(plr.Name.." has voted. they can no longer vote again!")
			end
			db = false
			
		elseif partname == "shaggypart" then

			db=true
			vals = vals + 1
			if hit.Parent:FindFirstChild("Humanoid") then
				shaggymanfacevote.SurfaceGui.TextLabel.Text = "votes: "..vals
				votingvalues.shaggyValue.Value = vals

				table.insert(plrName, plr.Name)
				print(plr.Name.." has voted. they can no longer vote again!")
			end
			db = false
		end
		end)
end
local replicatedStorage = game:GetService("ReplicatedStorage")
local votingValues = replicatedStorage:WaitForChild("votingValues")
local playersVoted = {}

local voteParts = {
	["manpart"] = {
		Part = workspace.manface,
		SurfaceGui = workspace.manfacevote.SurfaceGui,
		Value = votingValues.manValue,
	},
	["catpart"] = {
		Part = workspace.catmanface,
		SurfaceGui = workspace.catmanfacevote.SurfaceGui,
		Value = votingValues.catValue,
	},
	["shaggypart"] = {
		Part = workspace.shaggymanface,
		SurfaceGui = workspace.shaggymanfacevote.SurfaceGui,
		Value = votingValues.shaggyValue,
	},
}

for partName, partInfo in pairs(voteParts) do
	local part = partInfo.Part
	local surfaceGui = partInfo.SurfaceGui
	local value = partInfo.Value

	part.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player or playersVoted[player.Name] then
			return
		end

		if hit.Parent:FindFirstChild("Humanoid") then
			playersVoted[player.Name] = true
			local vals = value.Value + 1
			surfaceGui.TextLabel.Text = "votes: " .. vals
			value.Value = vals
		end
	end)
end
1 Like

thats absolutely perfect bro. tysm 4 spending the time to fix it. its 1m times better! :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.