What's wrong with my voting system?

Basically I am trying to make a voting system, through a Gui, but its not working and I have no idea why since there is no error message and I checked like 3 times the script for typos. And cannot find something like my problem on the devForum too. Anyways, here’s the code:


-- >> CONSTANTS << --
local voteTime = 25						-- In Seconds
local defaultMap = "Farmlnads"
local Votes = {
	["Farmlands"] = game.ReplicatedStorage.RemoteEvents.mapVotes.Farmlands.Value,
	["Islands"] = game.ReplicatedStorage.RemoteEvents.mapVotes.Islands.Value,
	["Rusia"] = game.ReplicatedStorage.RemoteEvents.mapVotes.Rusia.Value,
}

-- >> SERVICES << --
local runService = 				game:GetService("RunService")
local replicatedStorage =		game.ReplicatedStorage

-- >> VARIABLES << --
local highestVotes = -1
local chosenmap

local Farmlands = replicatedStorage.RemoteEvents.mapSelection.Farmlands
local Islands = replicatedStorage.RemoteEvents.mapSelection.Islands
local Rusia = replicatedStorage.RemoteEvents.mapSelection.Rusia

-- >> FUNCTIONS << --
local function returnMap()
	for i, v in pairs(Votes) do
		if v > highestVotes then
			highestVotes = v
			chosenmap = i
			return chosenmap
		end
	end
end

-- >> EVENTS << --
Farmlands.OnServerEvent:Connect(function()
	replicatedStorage.RemoteEvents.mapVotes.Farmlands.Value = replicatedStorage.RemoteEvents.mapVotes.Farmlands.Value + 1
end)

Islands.OnServerEvent:Connect(function()
	replicatedStorage.RemoteEvents.mapVotes.Islands.Value = replicatedStorage.RemoteEvents.mapVotes.Islands.Value + 1
end)

Rusia.OnServerEvent:Connect(function()
	replicatedStorage.RemoteEvents.mapVotes.Rusia.Value = replicatedStorage.RemoteEvents.mapVotes.Rusia.Value + 1
end)
4 Likes

I haven’t rlly taken a look directly into ur functions but there r a few type-os that can probably be the reason why your script isn’t working.

line 2 – you spelt “farmlands” wrong.

You also never called on the returnMap function, so you’d need to do a returnMap( ) somewhere.

First, make sure you’re calling the returnMap() function.

Also, I’m pretty sure you need to retrieve the values in the for loop itself. You only write the values into the table in the beginning of the script, meaning the table will always return them all as their beginning values (0 in this case)

So you’d instead do this:

local Votes = {
	["Farmlands"] = game.ReplicatedStorage.RemoteEvents.mapVotes.Farmlands,
	["Islands"] = game.ReplicatedStorage.RemoteEvents.mapVotes.Islands,
	["Rusia"] = game.ReplicatedStorage.RemoteEvents.mapVotes.Rusia,
}

for i, v in pairs(Votes) do
	if v.Value > highestVotes then
		highestVotes = v.Value
		chosenmap = i
		return chosenmap
	end
end
2 Likes

That’s why I had the remote events set, but they don’t work either when called by a localscript.

Update: Your method worked but now when I click on the first button being Farmlands it fires just the farmlands remote, and the other ones are not triggered at all even tho they are in the local script code.

1 Like

Any errors? Can you send the code here?

No there are no errors in the output and here the code for the local script. And yes I do realize its not very practical and has a bunch of duplicates.


-- >> CONSTANTS << --
local buttonDelay = 0.001

-- >> SERVICES << --
local replicatedStorage = 		game.ReplicatedStorage
local players = 				game:GetService("Players")
local runService = 				game:GetService("RunService")

-- >> VARIABLES << --
local mapSelection = script.Parent.mapSelection
local title = mapSelection.buttonLabel
local player = players.LocalPlayer

local FarmlandsEvent = replicatedStorage.RemoteEvents.mapSelection.Farmlands
local IslandsEvent = replicatedStorage.RemoteEvents.mapSelection.Islands
local RusiaEvent = replicatedStorage.RemoteEvents.mapSelection.Rusia

local lines = {
	line1 = mapSelection.lineDetail,
	line2 = mapSelection.lineDetail,
}

local Farmlands = mapSelection.Farmlands:WaitForChild("buttonLabel")
local Islands = mapSelection.Farmlands:WaitForChild("buttonLabel")
local Rusia = mapSelection.Farmlands:WaitForChild("buttonLabel")

-- >> FUNCTIONS << --
local function updateText()
	mapSelection.Farmlands.Text = "Farmlands (".. replicatedStorage.RemoteEvents.mapVotes.Farmlands.Value ..")"
	mapSelection.Farmlands.buttonLabel.Text = "Farmlands (".. replicatedStorage.RemoteEvents.mapVotes.Farmlands.Value ..")"
	mapSelection.Farmlands.buttonLabel.textShadow.Text = "Farmlands (".. replicatedStorage.RemoteEvents.mapVotes.Farmlands.Value ..")"
	
	mapSelection.Islands.Text = "Islands (".. replicatedStorage.RemoteEvents.mapVotes.Islands.Value ..")"
	mapSelection.Islands.buttonLabel.Text = "Islands (".. replicatedStorage.RemoteEvents.mapVotes.Islands.Value ..")"
	mapSelection.Islands.buttonLabel.textShadow.Text = "Islands (".. replicatedStorage.RemoteEvents.mapVotes.Islands.Value ..")"
	
	mapSelection.Rusia.Text = "Rusia (".. replicatedStorage.RemoteEvents.mapVotes.Rusia.Value ..")"
	mapSelection.Rusia.buttonLabel.Text = "Rusia (".. replicatedStorage.RemoteEvents.mapVotes.Rusia.Value ..")"
	mapSelection.Rusia.buttonLabel.textShadow.Text = "Rusia (".. replicatedStorage.RemoteEvents.mapVotes.Rusia.Value ..")"
end

--[[local function fireEvent()
	FarmlandsEvent:FireServer()
end]]--

-- >> EVENTS << --
runService.Heartbeat:Connect(function()
	wait(tonumber(buttonDelay))
	updateText()
end)

Farmlands.MouseButton1Click:Connect(function()
	wait(tonumber(buttonDelay))
	replicatedStorage.RemoteEvents.mapSelection.Farmlands:FireServer()
end)

Islands.MouseButton1Click:Connect(function()
	wait(tonumber(buttonDelay))
	replicatedStorage.RemoteEvents.mapSelection.Islands:FireServer()
end)

Rusia.MouseButton1Click:Connect(function()
	wait(tonumber(buttonDelay))
	replicatedStorage.RemoteEvents.mapSelection.Rusia:FireServer()
end)

(@CrafterPlaysCZ)

Hm, I’m not sure why the others wouldn’t work. One more thing would be to put a print inside the Clicked events to check if they are being registered at all. Could do the same for the OnServerEvents.

1 Like

So basically clicking the button triggers all of the remote events…

Fixed it, the problem was the fact that I mentioned the farmlands button as all the buttons and the other buttons didn’t trigger so here was my problem and thanks @CrafterPlaysCZ for helping me!

2 Likes

Oh, lol. Completely missed that. Np!