Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Map Voting System Displaying Duplicate Votes (Advanced?)

Asked by 4 years ago

So I have a map voting system where there is three pads, all named VotingPad1, VotingPad2, VotingPad3. When the voting time begins, each pad is linked to a board that displays the map name and the current live count of votes for that specific map. If you step on the pad, the board displays one vote. If you move to another pad, the old board says 0 and the new board says 1, etc etc. The issue I'm having here is with 2 or more people when they try to vote. For example, lets say we had Player1 and Player2 testing the system. Player1 steps on VotingPad1, the board connected to VotingPad1 displays one vote. Player2 steps on VotingPad2, now the 1 vote displayed on the board for VotingPad1 becomes 0 and the votes for VotingPad2 becomes 2, even though Player1 and Player2 are stepping on separate pads. The issue is the script is displaying their combined votes for some reason.

I have a server script inside ServerScriptService that runs all game functions through a module script, which is also located inside ServerScriptService. I then have a LocalScript inside StarterPlayerScripts handling all client functions, i.e stepping on the pad and sending the player's vote to the server.

Module Script:

function GameHandler:StartMapVoting()
    mapVotes = {}
    for i,v in next, coreFunction.VotingPads:GetChildren() do
        table.insert(mapVotes, {id = tonumber(v.Name:match("%d+")), mapName = GameHandler:GetMap(), players = {}})
    end
    rEvent:FireAllClients("StartVoting")
    gameSettings.voting = true
    GameHandler:DisplayTime()
    GameHandler:DisplayVotes()
end

function GameHandler:DisplayVotes()
    spawn(function()
        while gameSettings.voting do
            wait()
            for i,v in next, coreFunction.VotingPads:GetChildren() do
                local plrsVoting, mName
                for index, key in next, mapVotes do
                    if key.id == tonumber(v.Name:match("%d+")) then
                        plrsVoting = #key.players
                        mName = key.mapName
                        print(#key.players)
                        break
                    end
                end
                v.Screen.CountGui.TextLabel.Text = mName.. " - "..plrsVoting.." "..((plrsVoting == "1" and "Voter") or "Voter")
            end
        end
    end)
end

rEvent.OnServerEvent:Connect(function(plr, auth, Data)
    if auth == "VoteMap" then
        for i,v in next, mapVotes do
            for index, key in next, v.players do
                if key.player == plr.UserId then
                    table.remove(v.players, index)
                    break
                end
            end
        end
        for i,v in next, mapVotes do
            if v.id == Data.PadID then
                table.insert(v.players, {player = plr.UserId})
            end
        end
    elseif auth == "RemoveVote" then
        for i,v in next, mapVotes do
            for index, key in next, v.players do
                if key.player == plr.UserId then
                    table.remove(v.players, index)
                    break
                end
            end
        end
    end
end)

Local Script:

local function startMapVoting()
    local currentVote = nil
    for _, obj in next, coreFunction.VotingPads:GetChildren() do
        if obj:IsA("BasePart") and obj.Name:match("VotingPad") then
            table.insert(services, obj.Touched:Connect(function()
                local padID = tonumber(obj.Name:match("%d+"))
                if currentVote == nil then
                    currentVote = padID
                    rEvent:FireServer("VoteMap", {PadID = padID})
                elseif currentVote ~= padID then
                    currentVote = padID
                    rEvent:FireServer("VoteMap", {PadID = padID})
                end
            end))
        elseif currentVote ~= nil then
            currentVote = nil
            rEvent:FireServer("RemoteVote")
        end
    end
end

rEvent.OnClientEvent:Connect(function(auth, Data) 
    if auth == "StartVoting" then
        startMapVoting()
    elseif auth == "EndVoting" then
        for i,v in next, services do
            v:Disconnect()
        end
    end
end)

0
I attached a Gyazo link to further explain it in a video format, but looks like it didn't post. Here it is: https://gyazo.com/58df4def909c1d70d68fff9e3eb24d61 CalvinTLincoln 2 — 4y

Answer this question