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

Problem Bindable Event firing more than 1 time?

Asked by 3 years ago

So I am making a voting system for my game, that two scripts communicate with each other: the main game script and the voting script. In the voting script I have some code to run when a bindable event, "Voting", fires. The problem is that the bindable event is firing multiple times and the global variables bug.

Look:

local AvMinigames = game.ServerStorage.Minigames.Images:GetChildren()
local display = script.Parent.Tela.SurfaceGui.Tela
local votesBEV = game.ReplicatedStorage.voting
local buttons = script.Parent:GetChildren()
local PlayerLocks = {}
local VotesWithMinigames = {}
local PlayerVotes = {}
local ButTouchedEVS = {}
local displays = {}

local function beVisible(bool)
    if bool == true then
        display.Visible = true  
        for i, v in pairs(buttons)do
            v.SurfaceGui.Nome.Visible = true
        end
    else
        display.Visible = false
        for i, v in pairs(buttons)do
            v.SurfaceGui.Nome.Visible = false
        end
    end
end

for i, v in pairs(display:GetChildren()) do
    if v.ClassName == "ImageLabel" then
        displays[#displays + 1] = v
    end
end

for i, v in pairs(buttons) do
    if v.ClassName ~= "Part" then
        table.remove(buttons, i)
    end
end

for i, v in pairs(buttons) do
    if v.Name == "Tela" then
        table.remove(buttons, i)
    end
end

game.Players.PlayerRemoving:Connect(function(player)
    if table.find(PlayerLocks, player) ~= nil then
        local minigame = PlayerVotes[player]

        VotesWithMinigames[minigame] = VotesWithMinigames[minigame] - 1

        display[minigame]:WaitForChild("Votos").Text = VotesWithMinigames[minigame]

        PlayerVotes[player] = nil
    end
end)

votesBEV.Event:Connect(function(caso)
    AvMinigames = game.ServerStorage.Minigames.Images:GetChildren()
    if caso == 1 then
        beVisible(true)
        local function checktouch(part, button)
            if part.Parent:FindFirstChild("Humanoid") then
                local character = part.Parent
                if table.find(PlayerLocks, character.Name) == nil then
                    PlayerLocks[#PlayerLocks + 1] = character.Name

                    local minigame = button.SurfaceGui.Nome.Text

                    if VotesWithMinigames[minigame] == nil then

                        PlayerVotes[character.Name] = minigame

                        VotesWithMinigames[minigame] = 1

                        display[minigame]:WaitForChild("Votos").Text = VotesWithMinigames[minigame]

                    else
                        PlayerVotes[character.Name] = minigame

                        VotesWithMinigames[minigame] = VotesWithMinigames[minigame] + 1

                        display[minigame]:WaitForChild("Votos").Text = VotesWithMinigames[minigame]
                    end
                end
            end
        end


    for i, v in pairs(displays) do
        local imagenum = math.random(1, #AvMinigames)

        displays[i].Image = AvMinigames[imagenum].Image

        v.Name = AvMinigames[imagenum].Name

        script.Parent:FindFirstChild(i).SurfaceGui.Nome.Text = AvMinigames[imagenum].Name

        print("Image of minigame ".. AvMinigames[imagenum].Name.. " removed from the possibilities of minigames. ".. i)

        table.remove(AvMinigames, imagenum)

    end

    for i, v in pairs(buttons) do
        ButTouchedEVS[#ButTouchedEVS + 1] = v.Touched:Connect(function(touchedpart)
            checktouch(touchedpart, v)
        end)
    end

    elseif caso == 2 then
        beVisible(false)

        local MostVoted = {}
        local loop = 1

        VotesWithMinigames["Test"] = false

        for minigame, votes in pairs(VotesWithMinigames) do
            if minigame ~= "Test" then
                VotesWithMinigames["Test"] = true
                if loop == 1 then
                    MostVoted = {minigame, votes}
                else
                    if votes == MostVoted[2] then
                        if not #MostVoted > 2 then
                            MostVoted[3] = minigame

                            MostVoted[4] = votes

                        elseif #MostVoted == 4 then
                            MostVoted[5] = minigame

                            MostVoted[6] = votes
                        end
                    elseif votes > MostVoted[2] then
                        table.clear(MostVoted)

                        MostVoted = {minigame, votes}

                        print("Minigame ".. minigame.. " with ".. votes.. " votes.")
                    end
                end
            end

            for minigame, votes in pairs(VotesWithMinigames) do
                print(tostring(votes))
                if minigame == "Test"  and votes == false then
                    local random = math.random(1,3)

                    VotesWithMinigames["Test"] = nil

                    MostVoted[1] = buttons[random].SurfaceGui.Nome.Text

                    MostVoted[2] = 0

                    print("Voting not done. Random minigame chosen: ".. MostVoted[1])
                end
            end

            if #MostVoted > 2 then
                if #MostVoted == 4 then
                    local random = math.random(1,2)
                    if random == 1 then
                        table.remove(MostVoted, 3)

                        table.remove(MostVoted, 4)

                    else
                        table.remove(MostVoted, 1)

                        table.remove(MostVoted, 2)

                    end
                elseif #MostVoted == 6 then
                    local random = math.random(1,3)
                    if random == 1 then
                        table.remove(MostVoted, 3)

                        table.remove(MostVoted, 4)

                        table.remove(MostVoted, 5)

                        table.remove(MostVoted, 6)

                elseif random == 2 then
                    table.remove(MostVoted, 1)

                    table.remove(MostVoted, 2)

                    table.remove(MostVoted, 5)

                    table.remove(MostVoted, 6)

                elseif random == 3 then
                    table.remove(MostVoted, 1)

                    table.remove(MostVoted, 2)

                    table.remove(MostVoted, 3)

                    table.remove(MostVoted, 4)

                end
            end
        end

            print("Most voted minigame: ".. MostVoted[1].. " with ".. MostVoted[2].. " votes.")
            _G.minigamec = game.Workspace.Minijogos[MostVoted[1]]

            VotesWithMinigames = {}
            PlayerLocks = {}
            PlayerVotes = {}

        for i, v in pairs(displays)do
            v.Name = i
            v.Votos.Text = 0  -- Reseta os nomes das imagens e o número (visível) de votos --
        end
        end
    end
end)

You don't need to understand the script, it's fine in itself, but the problem is what the output is indicating:

   14:56:58.258 false  -  Server  -  Voto:144   14:56:58.259 Voting not done. Random minigame chosen: Sinking tiles  -  Server  -  Voto:154   14:56:58.259 1  -  Server  -  Voto:144   14:56:58.260 Most voted minigame: Sinking tiles with 0 votes.  -  Server  -  Voto:205   14:56:58.260 true  -  Server  -  Voto:144   14:56:58.260 Most voted minigame: Furnace jumps with 1 votes.  -  Server  -  Voto:205   14:56:58.475 false  -  Server  -  Voto:144   14:56:58.476 Voting not done. Random minigame chosen: Furnace jumps  -  Server  -  Voto:154   14:56:58.476 Most voted minigame: Furnace jumps with 0 votes.  -  Server  -  Voto:205

You see? The output indicates that the piece of code is running 2 or 3 times and the minigame selection bugs.

1
In this script you use the [votesBEV] variable once and that's to connect the event to function, where do you fire it from? You probably fire it multiple times from other script. imKirda 4491 — 3y
0
Sorry for not responding it for you, I fire it from an main script that just fires it once, bc it has an wait loop until one var is true bruno13bruno13 78 — 3y

Answer this question