The server keeps on thinking the value I'm trying to give it is a player

I am trying to make a voting system where the players press a button and send over what they voted for, however whenever I try and pass on the value for what they voted for the server keeps on thinking the value is the player

Script inside of button

local function Vote ()
	
	local player = game:GetService("Players").LocalPlayer
	local optionPicked = 1
	
	local function sendVote ()
		
		game:GetService("ReplicatedStorage").RemoteEvents.Vote:FireServer(player, optionPicked)
		
	end
	
	game:GetService("ReplicatedStorage").RemoteEvents.Vote.OnClientEvent:Connect(sendVote)
	
end

script.Parent.MouseButton1Down:Connect(Vote)

Script inside of serverScriptService managing the game

local ThemeManager = require(game:GetService("ServerStorage").ThemeManager)
local votes = {}

wait(5)
game:GetService("ReplicatedStorage").RemoteEvents.Vote:FireAllClients()

local function voteCount (player, optionPicked)
	
	table.insert(votes, optionPicked)
	print(optionPicked)
--print option picked keeps on printing out a player name
	
end

game:GetService("ReplicatedStorage").RemoteEvents.Vote.OnServerEvent:Connect(voteCount)

wait(1)
ThemeManager.Votes()

Module script

function Theme.Votes (votes)
	
	local option1Votes = 0
	local option2Votes = 0
	local option3Votes = 0
	local result
	
	for i,v in votes do
--an error keeps on happening here where it thinks the table votes is a nil value	
		if v == 1 then
			--1 is option 1
			option1Votes += 1
			
		elseif v == 2 then
			--2 is option 2
			option2Votes += 1
			
		elseif v == 3 then
			--3 is option 3
			option3Votes += 1
			
		end
		
	end
	--RESULTS
	
	if option1Votes > option2Votes and option3Votes then
		--1 is option 1
		result = 1
			
	elseif option2Votes > option1Votes and option3Votes then
		--2 is option 2
		result = 2
		
	elseif option3Votes > option2Votes and option1Votes then
		--3 is option 3
		result = 3
	--TIES
	elseif option1Votes == option2Votes then
		local tie = math.random(1,2)
		
		
		if tie == 1 then
			result = 1
		else
			result = 2
		end
		
	elseif option1Votes == option3Votes then
		local tie = math.random(1,2)
		

		if tie == 1 then
			result = 1
		else
			result = 3
		end
		
	elseif option2Votes == option3Votes then
		local tie = math.random(1,2)
		
		if tie == 1 then
			result = 2
		else
			result = 3
		end
		
	end
	
	return result
	
end

return Theme

Please help me the code has got 2 errors that I haven’t found in any other places.

Thanks in advance.

When you fire an event to the server, there is no need to pass the player with it, why? The server automatically sends the player as the first parameter when you connect to the OnServerEvent.

TLDR; dont send the Player when firing an event to the server.

-- // Local Script //
game:GetService("ReplicatedStorage").RemoteEvents.Vote:FireServer(optionPicked)

Thanks so much, I took out the player part and now the print prints what the player votes

1 Like

Well if I solved the problem, please mark my reply as the solution so if someone sees this, they can no what the solution was!

(That and then my solutions go up lol)

Glad to have helped :]

I would like to however that was not the only issue with the script as in the module script when it goes

for i,v in votes do

It thinks that it is iterating over nil,

Do I make a new post with this and mark yours as the solution or just wait on this post?

oh no, I can continue to help you! Give me one sec I gotta look over the code

Ok I think I see the issue, ThemeManager.Votes() Requires an argument, which is the table of votes, the problem is that you dont provide it!

The line in question:

-- // Fixed //
wait(1)
ThemeManager.Votes(votes)

Thank you so much , both my problems are fixed now

1 Like

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