Ok so I was trying to make a voting system, and I finished that, but now the intermission restarts after the last line code on the script below.
-- script in serverscriptservice
local text = game.ReplicatedStorage.Value
while true do
game.Workspace.Sound1:Play()
for i = 25,5,-1 do
text.Value = ("Intermission "..i)
wait(1)
if i == 5 then
game.ReplicatedStorage.WhichGamemode.Event:Connect(function(chosen)
text.Value = (chosen.." gamemode was picked!")
wait(5)
-- All the ends are below these lines of code
Thanks!
1 Like
I might need slightly more information to get this right. Does it restart after the for
loop reaches 5, or after the first loop at 25? Does it not wait(5)
after the event block is reached? Is the game mode event firing? Also, indentions, they are very important.
1 Like
Well like I said above, the script is fine until the last line of code above. It repeats the whole entire code again
1 Like
I see. That’s expected - It’s a while
loop and completely resets like its suppose to do.
Are you saying you want the while loop to end after the i == 5
conditional is met? if so, you can add a break
within that if
statement to stop it from repeating itself. Since this looks like a round based game, there is definitely a better option, but it’s hard to tell only looking at this portion of the code.
1 Like
I don’t want to leak all my code, and also I do want to repeat the whole code, but after all of my round code is done
1 Like
Fair enough. Did you try adding your round code within the if
statement? This could solve the while loop restarting as it will not restart until the round is over.
1 Like
You mean the rest of my code???
1 Like
Yes - there’s a way to do it with calls to functions so that you are not overwhelming the while
loop with information like this:
--- Your round based loop.
while true do
--- Something with 100+ lines of code.
end
When instead you can something something like this with functions:
function Something()
--- Something with 100+ lines of code.
end
-- Your round based loop.
while true do
wait(1)
Something()
end
1 Like
Wait actually it doesn’t loop at wait(5) part, but it keeps switching from the intermission to chosen gamemode. I will show you
1 Like
I’m trying to replicate the issue, but I’m not getting the same issues. The only thing I can think of is that the WhichGameMode
Event is actually scoped within the while
loop instead of having it’s own function, while the chosen variable is placed somewhere else. Do you mind show us the ends for this while
loop?
1 Like
''''
game.Workspace:WaitForChild("LoadedMap"):Destroy()
for _, player in pairs(game.Players:GetChildren()) do
if player.leaderstats.Kills.Value > 0 then
player.leaderstats.Kills.Value = 0
end
end
end
end
end
end
end
'''
These lines of code are at the end of my loop
I also tried this:
''''
local text = game.ReplicatedStorage.Value
local Maps = game.ServerStorage:GetChildren()
while true do
game.Workspace.Sound1:Play()
for i = 30,1,-1 do
text.Value = ("Intermission "..i)
wait(1)
if i == 1 then
game.ReplicatedStorage.WhichGamemode.Event:Connect(function(chosen)
if chosen == "Classic" then
print("Classic")
else
print("Teams")
end
end)
-- other script
game.ReplicatedStorage.WhichGamemode:Fire(chosen)
'''
But it ignored the printing part and moved on
Maybe I’m overlooking something, but this:
This is the culprit:
game.ReplicatedStorage.WhichGamemode.Event:Connect(function(chosen)
text.Value = (chosen.." gamemode was picked!")
wait(5)
When you connect an event this way, it performs on its own time, so the while
loop continues as if nothing happened.
2 Likes
Ok so what do I do instead? (30chars)
In the for loop inside of the while loop, check if i == 5 and then break
while true do
workspace.Sound1:Play()
for i = 25, 5, -1 do
text.Value = ("Intermission"..i)
if i == 5 then break end
end
end
game.ReplicatedStorage.WhichGamemode.Event:Connect(function(chosen)
text.Value = (chosen.." gamemode was picked!")
wait(5)
end)
Now it doesn’t show the gamemode text at all, it just restarts again.
Excuse me for taking so long.
@FKAGamingDeOne we established that he wanted to round code to work again once the rounds were over, so the break
statement wouldn’t work.
Since I noticed you are using separate scripts, you can use a BindableFunction
to replace the BindableEvent
and then use a variable to use for the chosen game mode like this:
Here is the entire script with notes - editing in.
--- The line underneath is what you will replace your current bindable event with.
game.ReplicatedStorage.WhichGamemode.OnInvoke = function(chosen)
--- Whatever code you have.
end
--- This function would go in the script that has yourmap picking code.
while true do
for i = 7,5,-1 do
print("Intermission "..i)
wait(.25)
if i == 5 then
print("Picking gamemode")
local c = game.ReplicatedStorage.WhichGamemode:Invoke()
--- I changed the line above into a variable. scripts wait for functions, not events.
print(c .. " gamemode was picked!")
wait(5)
game.Workspace:WaitForChild("LoadedMap"):Destroy()
for _, player in pairs(game.Players:GetChildren()) do
if player.leaderstats.Kills.Value > 0 then
player.leaderstats.Kills.Value = 0
end
end
end
end
end
Here’s the article on BindableFunctions
to make it easier for you to understand. Should be easy since you already know how to use BindableEvents
.
Have you reverted back to the original, and tried my code since that was the original problem?
Also that formatting has to be fixed, because I know most people will not help you when you publish the code on the forum like that; It makes it that much more harder to read, and the person helping you has to indent themselves if they want to test it themselves.
The only problem with the script is I want that script to know which game mode was chosen by the players