Boolean value keeps changing from false to true, and back again?
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):
02 | local ServerScriptService = game:GetService( "ServerScriptService" ) |
03 | local runService = game:GetService( "RunService" ) |
04 | local players = game:GetService( "Players" ) |
05 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
06 | local events = replicatedStorage:WaitForChild( "Events" ) |
07 | local roundEvent = events:WaitForChild( "RoundEvent" ) |
08 | local StatusEvent = events:WaitForChild( "StatusEvent" ) |
09 | local values = replicatedStorage:WaitForChild( "Values" ) |
10 | local ServerModules = ServerScriptService:WaitForChild( "Modules" ) |
11 | local Maps = require(replicatedStorage:WaitForChild( "Modules" ):WaitForChild( "MapsModule" )) |
12 | local VoteFunction = replicatedStorage:WaitForChild( "Events" ):WaitForChild( "VoteFunction" ) |
15 | local intermissionTime = 1 |
16 | local mapVotingTime = 15 |
17 | local roundStatus = values:WaitForChild( "RoundStatus" ) |
29 | roundStatus.Changed:Connect( function (val) |
30 | StatusEvent:FireAllClients(val) |
33 | VoteFunction.OnServerInvoke = function (plr, mapVoteName) |
35 | Votes [ mapVoteName ] + = 1 |
36 | print (Votes [ mapVoteName ] ) |
37 | return Votes [ mapVoteName ] |
42 | local function onPlayerEvent(player) |
44 | local playerAmount = #players:GetPlayers() |
45 | local minimumPlayerAmount = 2 |
46 | local enoughPlayers = playerAmount > = minimumPlayerAmount |
49 | game:GetService( "RunService" ).Heartbeat:Connect( function () |
56 | local function roundTimer() |
57 | while enoughPlayers do |
58 | for i = intermissionTime, 0 , - 1 do |
59 | roundStatus.Value = "Intermission: " ..i |
63 | roundStatus.Value = intermissionTime |
65 | for i = mapVotingTime, 0 , - 1 do |
66 | roundStatus.Value = "Voting: " ..i |
70 | roundStatus.Value = mapVotingTime |
74 | local timerCO = coroutine.wrap(roundTimer) |
77 | roundEvent:FireAllClients(enoughPlayers, minimumPlayerAmount, playerAmount) |
80 | local function waitingForPlayers() |
81 | local playersNeeded = minimumPlayerAmount - playerAmount |
82 | if playersNeeded = = 1 then |
83 | roundStatus.Value = "Not enough players to start a round. " ..playersNeeded.. " more player needed." |
85 | roundStatus.Value = "Not enough players to start a round. " ..playersNeeded.. " more players needed." |
94 | players.PlayerAdded:Connect(onPlayerEvent) |
95 | players.PlayerRemoving:Connect(onPlayerEvent) |
tanks for taking time
- boxy