Not the whole script (the addVote function is called but I cut it because it’s not important)
Client:
function addVote(player, vote)
for voteName, votes in pairs(Votes) do
if table.find(votes, player.UserId) then
table.remove(votes, table.find(votes, player.UserId))
UpdateVote:FireServer(voteName, -1)
break
end
end
table.insert(Votes[vote], player.UserId)
UpdateVote:FireServer(vote, 1)
end
UpdateVote.OnClientEvent:Connect(function(vote, amount)
print(vote, amount)
Background[vote]:WaitForChild("Votes").Text = tonumber(Background[vote]:WaitForChild("Votes").Text) + amount
end)
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UpdateVote = ReplicatedStorage:WaitForChild("UpdateVote")
UpdateVote.OnServerEvent:Connect(function(Player, vote, amount)
print(Player.Name, vote, amount)
UpdateVote:FireAllClients(vote, amount)
end)
Are you using the same event for server to client communication and client to server communication?
Remote events should only be used for one-way communication.
If you are looking into two way communication you should use two remote events. Or a RemoteFunction because I think they could be used for two-way communication.
Please check out this article about the proper use for RemoteFunctions and RemoteEvents.
RemoteEvent may not be used in two-way communication between the client and server.
An example is that the server can only send information through a remoteevent while a client can only recieve information through that same remoteevent. In this case, the client may not send information to the server through that RemoteEvent
In this case, you should make a RemoteEvent for a client to recieve information and a server to send information.
FireAllClients may be used by the server to send information to clients but may not be used by the server.
one to send information from the client to the server - You should use :FireServer() and event.OnServerEvent()
another to send information from the server to the client - You should use :FireClient() and event.OnClientEvent()
and finally one to send information from the server to other clients - You should use :FireAllClients as well as event.OnClientEvent
I am trying to tell you that the server and client cannot send each other information both ways (server to client and client to server) through the same event
This is incorrect. FireAllClients is only used by the server to send data to every client. Clients are not allowed to communicate between one another without going through the server for security reasons.
Sorry, but still, RemoteEvents are only for one-way communication and it is not working because the same remote event is being used to send data from a client to the server and the server to send data to all the clients.
Then why in that script is working(from other game):
client:
HardButton.MouseButton1Down:Connect(function()
if CanVote then
if findInTable(Player.Name) then
removeFromTable(Player.Name)
if PreviousVote ~= nil then
ChangeVotesEvent:FireServer(PreviousVote, -1)
end
ChangeVotesEvent:FireServer("Hard", 1)
else
if PreviousVote ~= nil then
ChangeVotesEvent:FireServer(PreviousVote, -1)
end
PreviousVote = "Hard"
ChangeVotesEvent:FireServer("Hard", 1)
end
end
end)
local EasyVotes = 0
local NormalVotes = 0
local HardVotes = 0
ChangeVotesEvent.OnClientEvent:Connect(function(amount, btn)
if btn == "Easy" then
EasyVotes += amount
EasyButton:WaitForChild("Votes").Text = tostring(EasyVotes)
elseif btn == "Normal" then
NormalVotes += amount
NormalButton:WaitForChild("Votes").Text = tostring(NormalVotes)
elseif btn == "Hard" then
HardVotes += amount
HardButton:WaitForChild("Votes").Text = tostring(HardVotes)
end
end)
server:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local ChangeVotesEvent = Remotes.ChangeVotes
ChangeVotesEvent.OnServerEvent:Connect(function(Player, amount, btn)
print(btn, amount)
ChangeVotesEvent:FireAllClients(btn, amount)
end)