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

Boolean value keeps changing from false to true, and back again?

Asked by 1 year ago

Problem: The code is for a round system, and has a variable which is used to determine if there are enough players in the game. The variable uses two other variables, the amount of players in the game and the minimum number of players needed to start a round, and makes sure the amount of players is greater than or equal to the minimum number of players needed. Since the minimum number of players is set to 2, when only one person joins the variable will correctly print "false". However, when there are 2 players, it keeps printing "true" and "false", and keeps printing that.

Solutions tried so far: There were no devforum topics about this. A solution that was attempted was to move the variables outside of the player added event on the script, but instead it kept printing false instead, even when there were 2 people.

Code (Is a Script):

--Variables
local ServerScriptService = game:GetService("ServerScriptService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local events = replicatedStorage:WaitForChild("Events")
local roundEvent = events:WaitForChild("RoundEvent")
local StatusEvent = events:WaitForChild("StatusEvent")
local values = replicatedStorage:WaitForChild("Values")
local ServerModules = ServerScriptService:WaitForChild("Modules")
local Maps = require(replicatedStorage:WaitForChild("Modules"):WaitForChild("MapsModule"))
local VoteFunction = replicatedStorage:WaitForChild("Events"):WaitForChild("VoteFunction")

--Values
local intermissionTime = 1
local mapVotingTime = 15
local roundStatus = values:WaitForChild("RoundStatus")
local Map1 = nil
local Map2 = nil
local Map3 = nil
local currentMap = nil
local Votes = {
    Map1Votes = 0;
    Map2Votes = 0;
    Map3Votes = 0
}


roundStatus.Changed:Connect(function(val)
    StatusEvent:FireAllClients(val)
end)

VoteFunction.OnServerInvoke = function(plr, mapVoteName)

    Votes[mapVoteName] += 1
    print(Votes[mapVoteName])
    return Votes[mapVoteName]
end



local function onPlayerEvent(player)
    --Dependent values
    local playerAmount = #players:GetPlayers()
    local minimumPlayerAmount = 2
    local enoughPlayers = playerAmount >= minimumPlayerAmount

    print(player)
    game:GetService("RunService").Heartbeat:Connect(function()
        print(enoughPlayers)
    end)

    print(enoughPlayers)
    print(playerAmount)

    local function roundTimer()
        while enoughPlayers do
            for i = intermissionTime, 0, -1 do
                roundStatus.Value = "Intermission: "..i
                task.wait(1)
            end

            roundStatus.Value = intermissionTime

            for i = mapVotingTime, 0, -1 do
                roundStatus.Value = "Voting: "..i
                task.wait(1)
            end

            roundStatus.Value = mapVotingTime
        end
    end

    local timerCO = coroutine.wrap(roundTimer)

    if enoughPlayers then
        roundEvent:FireAllClients(enoughPlayers, minimumPlayerAmount, playerAmount)
        timerCO()
    else
        local function waitingForPlayers()
            local playersNeeded = minimumPlayerAmount - playerAmount
            if playersNeeded == 1 then
                roundStatus.Value = "Not enough players to start a round. "..playersNeeded.." more player needed."
            else
                roundStatus.Value = "Not enough players to start a round. "..playersNeeded.." more players needed."
            end
        end
        waitingForPlayers()
    end
end



players.PlayerAdded:Connect(onPlayerEvent)
players.PlayerRemoving:Connect(onPlayerEvent)

tanks for taking time - boxy

Answer this question