I have a remote function that works perfectly fine on the first time its fired
local player = game.Players.LocalPlayer
print("sent")
script.Parent.Choice1.VOTE.MouseButton1Click:Connect(function()
if player.voted1.Value == false then
local choice = ("Votevalue11")
votere:FireServer(choice)
player.voted1.Value = true
end
end)
local function addvotes(player, choice)
print("rec")
game.Workspace.firstvotes:FindFirstChild(choice).Value = game.Workspace.firstvotes:FindFirstChild(choice).Value + 1
end
game.ReplicatedStorage:WaitForChild("Vote1").OnServerEvent:Connect(addvotes)
-- output
--> sent
--> rec
This works for the first time as I said but on the second time this remote is fired, this is the output:
-- output
--> sent
--> rec(x2)
Nothing about the script has changed from the first time to the next and yet for each time the remote is fired, the multiplier of “rec” goes up by one. As you can tell I’m using this for a vote system so this is making one players votes worth a way larger value than it’s supposed to be. How can I make it so it’s only received once like the first time off?
Not remote function its a remote event, script looks fine unless there are other parts of the script that is causing this issue, my suggestion is do a server sided check. Also maybe show the rest of the script.
You can use a variable and see if that fixes anything. Also, move the print.
local player = game.Players.LocalPlayer
local voted = false
script.Parent.Choice1.VOTE.MouseButton1Click:Connect(function()
if not voted then
print("sent")
local choice = ("Votevalue11")
votere:FireServer(choice)
voted = true
end
end)
I assume you want to prevent the remote from firing more than once?
Also, yes server sided checks. I was going to recommend that in a scrapped reply.
This is normal. See, your “sent” is not inside the click function, so it will only print once. Then, “rec” will print once, and then once more, as you fire the event twice.
That’s what I initially thought, but I believe it is a confusing way of saying that he doesn’t want the event to be able to fire more than once. See the debounce condition thing in the first script? The fact that he is able to get the 2x says that his debounce thing isn’t working.
This makes me think that the remote event is being fired multiple times on purpose. I don’t see any major issue with the code or the output. Though, if you handle the button debouce on the client, then a malicious person could spam the server with votes since the server is not checking.
I didn’t know about those vulnerabilities, I’ll have to look into that & sever side checking.
I tested the code again after moving the print to after the remote event is fired:
script.Parent.Choice1.VOTE.MouseButton1Click:Connect(function()
if player.voted1.Value == false then
local choice = ("Votevalue11")
votere:FireServer(choice)
print("sent")
player.voted1.Value = true
end
end)
And I’m still getting output like this on the second time around:
-- output
--> sent
--> rec(x2)
It still seems like the event is only being fired once.
I used the variable player.voted1.Value as a debounce so that it’s only possible to choose (and fire the event) once. Even after moving print(“sent”) to after votere:FireServer it only ever prints once.
If you’re only aiming for one vote for each player, such that if the player clicks on it the second time, it won’t work, you could probably make a debounce on the server so that it won’t repeat again.
script.Parent.Choice1.VOTE.MouseButton1Click:Connect(function()
if player.voted1.Value == false then
local choice = ("Votevalue11")
votere:FireServer(choice)
print("sent")
player.voted1.Value = true
end
end)
-- On the Client
local debounces = {}
game.ReplicatedStorage:WaitForChild("Vote1").OnServerEvent:Connect(function(player, choice)
if debounces[player] == nil then
debounces[player] = true
-- Sets so that it can only be received once
print("rec")
game.Workspace.firstvotes:FindFirstChild(choice).Value += 1
--Shorter way of adding 1 to the value
end
end)
-- use "debounces = {}" to reset the debounces