Code issues - Code isn't getting player vote

Hello, I would like to know what I am doing wrong and if someone can help me
I am creating a voting system and I need to take the vote of each player by pressing the button and then the number of their vote is shown in the textlabel

local character = game.Workspace.Player
local player = game.Players:GetPlayerFromCharacter(character)

script.Parent.votere.OnServerEvent:Connect(function(player)
	local votenumber = script.Parent.valvote.Value
	
	votenumber = player+1
	print(votenumber)
	
	local text = script.Parent.TextLabel
	text.Text = "vote number: "..tostring(votenumber)
end)

image

image

I would use tables for voting systems. What’re you doing here? Not sure if I’m wrong but I don’t think you can just do “+1” on a player instance.

votenumber = player+1

You can just do votenumber = votenumber + 1 and then make a check if the player already voted so if the player tries voting again it won’t count

1 Like

there’s an error:
Player is not a valid member of Workspace “Workspace” -

image

I’m beyond confused for this part, you can’t get the local player from the server, nor does “Player” exist in workspace unless you made an object for it

local voted = {} -- table to hold the players who voted

script.Parent.votere.OnServerEvent:Connect(function(player)
    if table.find(voted, player.UserId) then return end -- the player already voted
    table.insert(voted, player.UserId) -- add the player to a table

	local votenumber = script.Parent.valvote -- don't do ".Value" because it won't update anything
	
	votenumber.Value += 1 -- bascially votenumber.Value = votenumber.Value + 1 but easier
	print(votenumber.Value)
	
	local text = script.Parent.TextLabel
	text.Text = "vote number: "..tostring(votenumber.Value)
end)
2 Likes

The player variable was defined after that script so it will error, and you wouldn’t need the player variable above anyway since you used a remote event only to get the player (which might be a bad idea) there probably won’t be a player in your game with the username of “player”

1 Like

Great, it works! How could I do it if there are 3 buttons? :scream:

Essentially the same thing but use different tables

local table1 = {}
local table2 = {}
-- .. etc

Use one .OnServerEvent but use RemoteEvent:FireServer() for the local scripts that control the buttons when they’re pressed

2 Likes

THAANKS YOU SO MUCH !! It had been very difficult for me to make this system :frowning:

2 Likes