How can I fix my minigame script?

So basically this is script works pretty much like a minigame script, with the difference that the focus is only on racing.

  1. What do you want to achieve?
    I want the Main script to have a focus on the 3 first persons to cross the finish line (touch the part) if the Winner (first person to cross the line) finish the race and the time is not over then “do stuff”. Basically the Ingame part of the main script will focus on the first person to finish the race and the remaining time left to decide what will happen next.

  2. What is the issue? Include screenshots / videos if possible!

I’ve tried doing what i said above but don’t know where i did it wrong, i’ve been trying to mess around with the “end” changing it’s position to see what happens, but it always skips a piece of code whenever i change them.

Here is the script:

local vals = game.ReplicatedStorage.vals
t = 0
while true do
	wait(1)
	local plrs = game.Players:GetChildren()
	if #plrs > 0 then
	t = 10
	repeat
		t = t-1
		s.Value = "Intermission.. "..t
		wait(1)
	until t == 0
	s.Value = "Game starting!"
	wait(2)
	local mapselect = game.ReplicatedStorage.Games:GetChildren()
	local choose = math.random(1, #mapselect)
	curnum = 0
	for i =1,#mapselect do
		curnum = curnum +1
		if curnum == choose then
			mapselect[i]:Clone().Parent = workspace
			curmap = mapselect[i].Name
			s.Value = "Next map is "..mapselect[i].Name
		end
	end
	wait(3)
	local plrs = game.Players:GetChildren()
	for i = 1,#plrs do
		local num = math.random(1,32)
		plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
		plrs[i].Character.Parent = workspace.Ingame		
	end
	t=20 -- Round starting time
	
	repeat t = t-1 -- FIRST PLACE START --
		s.Value = t.." seconds left"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
	until t <= 0 or vals.Winner.Value ~= "" or #ingame == 0
	
	if vals.Winner.Value ~= "" then -- If the Winner (Firtst Place) exists then:
		s.Value = vals.Winner.Value.. " finished first!"
		local plrs = game.Players:GetChildren()
		for i = 1,#plrs do
		game.Players[vals.Winner.Value].leaderstats.Points.Value = game.Players[vals.Winner.Value].leaderstats.Points.Value +50
		game.Players[vals.Winner.Value].leaderstats.Wins.Value = game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
		 -- FIRST PLACE END --
		
		wait(1)
		 
		 repeat t = t-1 -- SECOND PLACE START -- 
		s.Value = t.." seconds left"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
		 until t <= 0 or vals.Second.Value ~= "" or #ingame == 0
		
		if vals.Second.Value ~= "" then -- If the Second place exists then:
		s.Value = vals.Second.Value.. " finished second!"
		local plrs = game.Players:GetChildren()
		for i = 1,#plrs do
		game.Players[vals.Second.Value].leaderstats.Points.Value = game.Players[vals.Second.Value].leaderstats.Points.Value +25
		 -- SECOND PLACE END --
		
		wait(1)
		 
		 repeat t = t-1 -- THIRD PLACE START --
		s.Value = t.." seconds left"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
		   until t <= 0 or vals.Third.Value ~= "" or #ingame == 0
		
		if vals.Third.Value ~= "" then -- If the Second place exists then:
		s.Value = vals.Third.Value.. " finished third!"
		local plrs = game.Players:GetChildren()
		for i = 1,#plrs do
		game.Players[vals.Third.Value].leaderstats.Points.Value = game.Players[vals.Third.Value].leaderstats.Points.Value +25
		 end-- THIRD PLACE END --
		
		wait(1)
		
	repeat t = t-1
		s.Value = t.. " remaining to everyone else!"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
	until t <= 0 or #ingame == 0 and vals.Winner.Value ~= ""
        s.Value = "Race is over!"
		vals.Winner.Value = ""
		vals.Second.Value = ""
		vals.Third.Value = ""
			else
		s.Value = "No one has won!"
		vals.Winner.Value = ""
		vals.Second.Value = ""
		vals.Third.Value = ""	
		end
		end
	wait(3)
	local ingame = workspace.Ingame:GetChildren()
	for i =1,#ingame do
		local plr = game.Players:GetPlayerFromCharacter(ingame[i])
		plr:LoadCharacter()
		end
	workspace[curmap]:Destroy()
	wait(1)		
		end
		end

		else
		s.Value = "Waiting for 2 players to start."
		wait(1)
	end
		end
		end

i don’t get anything on the output btw, it just skips pieces of code

1 Like

What piece of code is it skipping? Is it anything specific?

1 Like

The skipping issues starts on this part of the script:

		
	repeat t = t-1
		s.Value = t.. " remaining to everyone else!"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
	until t <= 0 or #ingame == 0 and vals.Winner.Value ~= ""
        s.Value = "Race is over!"
		vals.Winner.Value = ""
		vals.Second.Value = ""
		vals.Third.Value = ""
			else
		s.Value = "No one has won!"
		vals.Winner.Value = ""
		vals.Second.Value = ""
		vals.Third.Value = ""	
		end
		end
	wait(3)
	local ingame = workspace.Ingame:GetChildren()
	for i =1,#ingame do
		local plr = game.Players:GetPlayerFromCharacter(ingame[i])
		plr:LoadCharacter()
		end
	workspace[curmap]:Destroy()
	wait(1)		
		end
		end

		else
		s.Value = "Waiting for 2 players to start."
		wait(1)
	end
		end
		end

Depending on what “end” i change positions this piece of code works:

	for i =1,#ingame do
		local plr = game.Players:GetPlayerFromCharacter(ingame[i])
		plr:LoadCharacter()
		end
	workspace[curmap]:Destroy()
	wait(1)		
		end
		end

		else
		s.Value = "Waiting for 2 players to start."
		wait(1)
	end
		end
		end

But never this one, this part always skips:

		s.Value = t.. " remaining to everyone else!"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
	until t <= 0 or #ingame == 0 and vals.Winner.Value ~= ""
        s.Value = "Race is over!"
		vals.Winner.Value = ""
		vals.Second.Value = ""
		vals.Third.Value = ""
			else
		s.Value = "No one has won!"
		vals.Winner.Value = ""
		vals.Second.Value = ""
		vals.Third.Value = ""	
		end
		end
1 Like

Usually when “skipping” issues arise, I like to use print(value) to determine which check is failing

local FailedValue = false

print(FailedValue)

if FailedValue then
    print("Success")
end

Once you diagnose where the check failed, you can trace back to all the parts of the script that deals with the failed value and determine where it failed, like this:

local FailedValue = true
print(FailedValue) --Ok, everything is alright

local FailedValue = false
print(FailedValue) --Uh oh!

if FailedValue then
    print("Success!")
end

Hope this helps!

3 Likes

Sorry, but i am still learning how to script properly, can you give me a example of how can i use this on my script?

Simply use the print function to test if this line of code is being played or not. Take this scenario for example…

Lets say I have a statement which picks a random number 1 - 3
Then I detect which number has been pressed
Ill detect this by printing in the console :slight_smile:

local number = math.random(1,3)

if number == 1 then
print("its one"
elseif number == 2 then
print("two")
elseif number == 3 then
print("its three")
end

Now I can see which lines of code got ran in the if statement by simply seeing what got printed in the console

Hope this helped!

2 Likes

I just updated my reply, hopefully that answers your question!

So, i’ve tried doing what you’ve said and here is what i get:

Even though the idea of my game is to be playable only if there is 2 or more people on the server, i want the script to work properly even if there is only 1 person playing, SO the script works only to this point when the Winner value is defined:

		s.Value = vals.Winner.Value.. " finished first!"
		local plrs = game.Players:GetChildren()
		for i = 1,#plrs do
		game.Players[vals.Winner.Value].leaderstats.Points.Value = game.Players[vals.Winner.Value].leaderstats.Points.Value +50
		game.Players[vals.Winner.Value].leaderstats.Wins.Value = game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
		print("First Place Over") -- FIRST PLACE END --

It prints “First Place Over” and seems like it freezes and start the script all over skipping all the rest of the script

It is most likely getting stuck in this repeat statement…

repeat t = t-1 -- SECOND PLACE START -- 
		s.Value = t.." seconds left"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
		 until t <= 0 or vals.Second.Value ~= "" or #ingame == 0

Try printing something into the console to log when the repeat statement has ended

1 Like

If its not the repeat statement then its the if statement corresponding to it.
Once again figuring this out is simply just testing the print statement after said condition has ran to verify it has actually ran…

1 Like

Well, i am doing this, see if i can save myself out of this mess i made xD

if i can fix it i’ll let you guys know, thanks for the help everyone

1 Like