Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I made a voting system but only the first player's vote is counted?

So I made a GUI voting system, and this GUI is placed into each player's PlayerGui and in each copy of this GUI is the local script which I will post below. For some reason only the first person's vote is counted.

So what happens is all the player get the GUI, and when the first person votes, it changes the value inside of the workspace, and adds a vote to it. Then for every other player that votes, nothing happens. The value isn't added. Not sure why.

The script is basically the same event 3 times in a row, for each vote button. The actual part that doesn't work is at the bottom.

local map1 = script.Parent.Map1
local map2 = script.Parent.Map2
local map3 = script.Parent.Map3

map1vote = map1.MouseButton1Click:connect(function()
	if script.Parent.Vote.Value == "N/A" then
		script.Parent.Vote.Value = map1.MapName.Text
		map1.BorderColor3 = Color3.new(0, 170, 0)
	elseif script.Parent.Vote.Value == map2.MapName.Text then
		script.Parent.Vote.Value = map1.MapName.Text
		map1.BorderColor3 = Color3.new(0, 0, 0)
		map2.BorderColor3 = Color3.new(0, 0, 0)
		map3.BorderColor3 = Color3.new(0, 0, 0)
		
		map1.BorderColor3 = Color3.new(0, 170, 0)
	elseif script.Parent.Vote.Value == map3.MapName.Text then
		script.Parent.Vote.Value = map1.MapName.Text
		map1.BorderColor3 = Color3.new(0, 0, 0)
		map2.BorderColor3 = Color3.new(0, 0, 0)
		map3.BorderColor3 = Color3.new(0, 0, 0)
		
		map1.BorderColor3 = Color3.new(0, 170, 0)
	end
end)

map2vote = map2.MouseButton1Click:connect(function()
	if script.Parent.Vote.Value == "N/A" then
		script.Parent.Vote.Value = map2.MapName.Text
		map2.BorderColor3 = Color3.new(0, 170, 0)
	elseif script.Parent.Vote.Value == map1.MapName.Text then
		script.Parent.Vote.Value = map2.MapName.Text
		map1.BorderColor3 = Color3.new(0, 0, 0)
		map2.BorderColor3 = Color3.new(0, 0, 0)
		map3.BorderColor3 = Color3.new(0, 0, 0)
		
		map2.BorderColor3 = Color3.new(0, 170, 0)
	elseif script.Parent.Vote.Value == map3.MapName.Text then
		script.Parent.Vote.Value = map2.MapName.Text
		map1.BorderColor3 = Color3.new(0, 0, 0)
		map2.BorderColor3 = Color3.new(0, 0, 0)
		map3.BorderColor3 = Color3.new(0, 0, 0)
		
		map2.BorderColor3 = Color3.new(0, 170, 0)
	end
end)

map3vote = map3.MouseButton1Click:connect(function()
	if script.Parent.Vote.Value == "N/A" then
		script.Parent.Vote.Value = map3.MapName.Text
		map3.BorderColor3 = Color3.new(0, 170, 0)
	elseif script.Parent.Vote.Value == map1.MapName.Text then
		script.Parent.Vote.Value = map3.MapName.Text
		map1.BorderColor3 = Color3.new(0, 0, 0)
		map2.BorderColor3 = Color3.new(0, 0, 0)
		map3.BorderColor3 = Color3.new(0, 0, 0)
		
		map3.BorderColor3 = Color3.new(0, 170, 0)
	elseif script.Parent.Vote.Value == map2.MapName.Text then
		script.Parent.Vote.Value = map3.MapName.Text
		map1.BorderColor3 = Color3.new(0, 0, 0)
		map2.BorderColor3 = Color3.new(0, 0, 0)
		map3.BorderColor3 = Color3.new(0, 0, 0)
		
		map3.BorderColor3 = Color3.new(0, 170, 0)
	end
end)

wait(9)
map1vote:Disconnect()
map2vote:Disconnect()
map3vote:Disconnect()
if script.Parent.Vote.Value == "N/A" then
	print("It's N/A")
else
	game.Workspace[script.Parent.Vote.Value].Value = game.Workspace[script.Parent.Vote.Value].Value + 1
end
print(game.Workspace[script.Parent.Vote.Value].Value)
wait(1)
script.Parent.Parent:Destroy()

1 comment

0

Just a moment while I type my reply.

1 answer

✓ Accepted answer

Hi fireflame, and thanks for coming to scriptinghelpers!

From what I'm reading, it would appear to be a map voting GUI. With this in mind, let me show you what was wrong with the script.

In ROBLOX, there is a Server/Client relationship. A client (Your ROBLOX window, the PC that you are on) has restricted access to the server. With this in mind, I am going to make the assumption that your game has FilteringEnabled turned on. FilteringEnabled,briefly described, prevents any changes made to the server by a client from being Replicated/Sent to other clients.

So, for example, if I were to execute this code from a local script :

local h=Instance.new('Part',game.Workspace)

A brick would show for you, but not for other clients. This is because it wasn't Replicated/Sent.

Now that we know this, if your game has FilteringEnabled set to true, changing the values inside the Workspace will remain LOCAL to you; the server and other clients will still see an empty value. Let it also be known that storing values inside the Workspace is bad practice, as exploiters can tamper with the values if FilteringEnabled is set to false.

Try this code instead (Skeleton code,GUI animations and other misc. code not included):

Let's Assume there are 3 values in ServerStorage with the names "Map1", "Map2" and "Map3".

Let's also assume that there is a RemoteEvent in ReplicatedStorage named "MapVoteEvent".

ServerScript:

--Just defining variables,and referencing them to objects.
local Map1=game.ServerStorage.Map1
local Map2=game.ServerStorage.Map2
local  Map3=game.ServerStorage.Map3
local VoteEvent=game.ReplicatedStorage.MapVoteEvent
-------------

--The function that handles map voting while map voting is running.
function MapVote(ClientThatFired,MapName)
if MapName=="Map1" then Map1.Value=Map1.Value+1
if MapName=="Map2" then Map2.Value=Map2.Value+1
if MapName=="Map3" then Map3.Value=Map3.Value+1
end

local ClientVoted=VoteEvent.OnServerEvent:connect(MapVote)

wait(10) --How long should voting be allowed?

--In here,remove all the voting GUIs from users,since voting is finished.
--Also load the map and other things.

ClientVoted:disconnect() --We no longer need this event,disconnect it so the server ignores it.

LocalScript (Again,bare skeleton code,for help purposes):

Also assume that the localscript's parent contains the Map vote buttons.


--Defining variables with reference to the vote buttons.
MapButton1=script.Parent.Map1
MapButton2=script.Parent.Map2
MapButton2=script.Parent.Map2
VoteEvent=game.ReplicatedStorage.MapVoteEvent
Voted=false --Basically a debounce,only allows the player to vote once!
--------------------

MapButton1.MouseButton1Click:connect(function()
	if Voted==false then
	VoteEvent:FireServer("Map1")
	Voted=true
	end
end)

MapButton2.MouseButton2Click:connect(function()
	if Voted==false then
	VoteEvent:FireServer("Map2")
	Voted=true
	end
end)

MapButton3.MouseButton3Click:connect(function()
	if Voted==false then
	VoteEvent:FireServer("Map3")
	Voted=true
	end
end)

Please note,that this code is not the most secure. It is solely for help purposes.

Hope this helps,and be sure to mark it as the answer!

-Reshiram110

1 comment

0

MouseButton2 and 3 click existZ?

This discussion is preserved as it was. New answers, comments, and votes are closed.