Hi,
I’m having a issue with my games round being fired more than once. I think this is happening because
is being run by how many people are in the game. if theres 2 people, it will run twice. How do I fix this?
Client:
local function chooseEvent()
if not ReplicatedStorage:GetAttribute(Variables.StatusAttribute) then
return
end
local strings = string.split(ReplicatedStorage:GetAttribute(Variables.EventAttribute), " ")
local event, difficulty = strings[1], strings[2]
for i = 1,10 do
if i ~= 10 then
local randomEvent = math.random(1, #Events)
local randomDifficulity = math.random(1, #Variables.Difficulties)
local difficulty = Variables.Difficulties[randomDifficulity]
local event = Events[randomEvent]
Difficulty.Text = "DIFFICULTY: "..string.format(Variables[difficulty], difficulty)
Event.Text = tostring(Events[randomEvent])
else
Difficulty.Text = "DIFFICULTY: "..string.format(Variables[difficulty], difficulty)
Event.Text = event
end
Transparency:set(1, true)
task.wait(0.1)
Transparency:set(0, true)
task.wait(0.1)
if i == 10 then
Audio.play(SoundService.Master.Interface.Chosen)
task.wait(3)
Transparency:set(1, true)
if not ReplicatedStorage:GetAttribute(Variables.StatusAttribute) then
return
end
BeginEvent:FireServer()
end
end
end
-- this fires when the game intro finishes (beginevent is not fired)
ReplicatedStorage:GetAttributeChangedSignal(Variables.IntroAttribute):Connect(function(...: any)
if not ReplicatedStorage:GetAttribute(Variables.IntroAttribute) then
print("intro")
chooseEvent()
end
end)
-- this fires when a new event is needed to be chosen (after a round end)
BeginEvent.OnClientEvent:Connect(function(...: any)
print("event fired")
chooseEvent()
end)
Server:
local function getRandomEvent()
local randomEvent, randomDifficulty = math.random(1, #Events:GetChildren()), math.random(1, #Variables.Difficulties)
local event, difficulty = Events:GetChildren()[randomEvent]:: ModuleScript, Variables.Difficulties[randomDifficulty]
ReplicatedStorage:SetAttribute(Variables.EventAttribute, tostring(event).. " " .. difficulty)
return event, difficulty
end
local function isGameOver() -- ends game if everyone is dead
if #Participants == 0 then
ReplicatedStorage:SetAttribute(Variables.StatusAttribute, false)
ReplicatedStorage:SetAttribute(Variables.TextAttribute, Variables.DeadText)
beginEvent:Disconnect()
for _, v in pairs(Variables.Cache:GetChildren()) do -- clear cache
v:Destroy()
end
task.spawn(function()
task.wait(Variables.GAME_END_DELAY)
GameService.RoundAsync() -- redo intermission
end)
return true
else
return false
end
end
function GameService.RoundAsync()
-- intermission
for seconds = Variables.Intermission, 0, -1 do
ReplicatedStorage:SetAttribute(TextAttribute, string.format(Variables.IntermissionText, seconds))
task.wait(1)
end
-- insert participants in table
for _, participant in pairs(Players:GetPlayers()) do
table.insert(Participants, participant)
-- connecting participants death event and removing them upon death
if participant.Character then
local humanoid = participant.Character.Humanoid :: Humanoid
local humanoidDied = nil :: RBXScriptConnection?
humanoidDied = humanoid.Died:Connect(function()
table.remove(Participants, table.find(Participants, participant))
humanoidDied:Disconnect()
if isGameOver() then return end
end)
end
task.spawn(function() -- seperate thread to not block for loop
task.wait(Variables.TELEPORT_DELAY)
-- teleport players
local root = participant.Character.HumanoidRootPart
root.CFrame = Variables.Map.Spawn.CFrame + Vector3.new(0, 5, 0)
end)
end
-- change game status to began
ReplicatedStorage:SetAttribute(Variables.StatusAttribute, true)
local event, difficulty = getRandomEvent() -- get a random event
beginEvent = BeginEvent.OnServerEvent:Connect(function(player: Player, ...: any)
print("event start")
event, difficulty = getRandomEvent() -- chooses another random event
require(event).StartAsync(difficulty)
-- startasync yields code until event is done
if #Participants == 0 then return end
print("firing")
BeginEvent:FireAllClients() -- tell the client to start the next event
end)
end
note: difficulty and event is chosen on server, the client side is just ui and tells the server when to start the next event