Slap Battles tournament GUI

Ok so I’m working on my own “slap battles clone game” and I’m making the tournament where a GUI will appear on all players screens and if they click “NO” it will disappear but if they click “YES” it will teleport them to an arena where they will compete against other players and whoever wins will get a certain amount of currency.

The problem is that I don’t know how to do this very well… I have already made the GUI and a (VERY BADLY MADE) script, but it doesn’t work at all.

Can anyone help me with this??

Here is what I have so far:

local ui = script.Tournament

local players = {}

local tournamenton = false

while wait(10) do
for i,v in pairs(game.Players:GetChildren()) do
local c = ui:Clone()
c.Parent = v.PlayerGui
local bonksamountwin = math.random(100, 250)
c.OuterFrame.Prize.Text = “A bonk challenge is starting with a prize of " … bonksamountwin … " bonks.”

	c.OuterFrame.YES.MouseButton1Click:Connect(function()
		c:Destroy()
		wait(8)
		tournamenton = true
		local playersamount = #players
		print(playersamount)
		
		table.insert(players, v.Name)
		local plr = v
		print(plr.Name)
		c:Destroy()
		
		plr.Character:MoveTo(workspace.Tournament.tp.Position)
		plr.Character.Humanoid.Died:Connect(function()
			local index = table.find(players, plr.Name)
			table.remove(players, index)
		end)
		
		while wait(1) do
			if playersamount == 1 then
				for i,v in pairs(game.Players:GetChildren()) do
					local winner = script.WINNER:Clone()
					winner.WINNER.Visible = true
					winner.WINNER.Text = plr.Name .. " HAS WON " .. bonksamountwin .. " BONKS!"
					plr.Character.Humanoid.Health = 0
					plr.leaderstats.Bonks.Value += bonksamountwin
					
					wait(2)
					
					winner:Destroy()
					
					tournamenton = false
				end
			end
		end
	end)
	
	
	c.OuterFrame.NO.MouseButton1Click:Connect(function()
		c:Destroy()
	end)
	
	wait(7)
	
	c:Destroy()
end

end``

1 Like

I love it when the text formats incorrectly. Sorry.

Format code like this:

print("Hello world!")

image

What does the Gui look like? What’s it made of? Is the script you showed server sided, or client sided?

server sided and and the GUI format is like this:

image

There is no client sided scripts so far but I will probably need them soon for the music and sounds that only play for players who click “YES”

1 Like

I cleaned it up a bit and made it to where you can break out of the loop. Still not sure if this will work but you can give it a try.

c.OuterFrame.YES.MouseButton1Click:Connect(function()
    c:Destroy()

    tournamenton = true
    local playersamount = #players
    print(playersamount)

    if playersamount > 0 then
        local plr = players[1]  -- Assuming you want the first player in the table

        plr.Character:MoveTo(workspace.Tournament.tp.Position)
        plr.Character.Humanoid.Died:Connect(function()
            local index = table.find(players, plr)
            table.remove(players, index)
        end)

        while wait(1) do
            if #players == 1 then
                local winner = script.WINNER:Clone()
                winner.WINNER.Visible = true
                winner.WINNER.Text = plr.Name .. " HAS WON " .. bonksamountwin .. " BONKS!"
                plr.Character.Humanoid.Health = 0
                plr.leaderstats.Bonks.Value += bonksamountwin

                wait(2)

                winner:Destroy()

                tournamenton = false
                break  -- Break out of the loop when a winner is determined
            end
        end
    end
end)

c.OuterFrame.NO.MouseButton1Click:Connect(function()
    c:Destroy()
end)

-- Additional logic...

wait(7)

c:Destroy()

Thanks so much for helping me to clean up the script! Do you have any ideas why its still not working? Do I need more players in the server other than me? I’m testing right now but if thats the case thats completely fine…

So, what is happening when you click the yes button, is it giving any errors?

Nope, none at all I checked console on both client and server

I would suggest to start adding print statments everywhere, and see which ones print and which ones don’t, that is usually how I figure out a bug like this one.

All done, heres the new script with prints included:

Strangely it only prints up to the “tournament is on” part and then never prints the “player moved to tournament” bit… **
image


local players = {}

local tournamenton = false

while wait(10) do
	for i,v in pairs(game.Players:GetChildren()) do
		local c = ui:Clone()
		print("ui cloned")
		c.Parent = v.PlayerGui
		local bonksamountwin = math.random(100, 250)
		c.OuterFrame.Prize.Text = "A bonk challenge is starting with a prize of " .. bonksamountwin .. " bonks."
		print("bonk amount set")
		
		c.OuterFrame.YES.MouseButton1Click:Connect(function()
			print("clicked yes option")
			c:Destroy()

			tournamenton = true
			print("tournament is on")
			local playersamount = #players
			print(playersamount)

			if playersamount > 0 then
				print("player moved to tournament")
				
				local plr = players[1]  -- Assuming you want the first player in the table

				plr.Character:MoveTo(workspace.Tournament.tp.Position)
				plr.Character.Humanoid.Died:Connect(function()
					local index = table.find(players, plr)
					table.remove(players, index)
				end)

				while wait(1) do
					if #players == 1 then
						print("player won")
						local winner = script.WINNER:Clone()
						winner.WINNER.Visible = true
						winner.WINNER.Text = plr.Name .. " HAS WON " .. bonksamountwin .. " BONKS!"
						plr.Character.Humanoid.Health = 0
						plr.leaderstats.Bonks.Value += bonksamountwin

						wait(2)

						winner:Destroy()

						tournamenton = false
						break  -- Break out of the loop when a winner is determined
					end
				end
			end
		end)

		c.OuterFrame.NO.MouseButton1Click:Connect(function()
			print("clicked no option")
			c:Destroy()
		end)

		-- Additional logic...

		wait(7)

		c:Destroy()
	end
end ```

Here I made some additional changes, make sure this version of the script is running on the server

local players = {}
local tournamenton = false

while wait(10) do
    players = {}  -- Clear the players table

    for i, player in pairs(game.Players:GetPlayers()) do
        local c = ui:Clone()
        c.Parent = player.PlayerGui

        local bonksamountwin = math.random(100, 250)
        c.OuterFrame.Prize.Text = "A bonk challenge is starting with a prize of " .. bonksamountwin .. " bonks."

        c.OuterFrame.YES.MouseButton1Click:Connect(function()
            c:Destroy()
            tournamenton = true
            players = {player}  -- Adds the player to the table
            player.Character:MoveTo(workspace.Tournament.tp.Position)

            player.Character.Humanoid.Died:Connect(function()
                local index = table.find(players, player)
                table.remove(players, index)
            end)

            while wait(1) do
                if #players == 1 then
                    local winner = script.WINNER:Clone()
                    winner.WINNER.Visible = true
                    winner.WINNER.Text = player.Name .. " HAS WON " .. bonksamountwin .. " BONKS!"
                    player.Character.Humanoid.Health = 0
                    player.leaderstats.Bonks.Value += bonksamountwin
                    wait(2)
                    winner:Destroy()
                    tournamenton = false
                    break
                end
            end
        end)

        c.OuterFrame.NO.MouseButton1Click:Connect(function()
            c:Destroy()
        end)

        wait(7)
        c:Destroy()
    end
end

Uhm, I’m almost certain this is an issue with the game design and not the script but for whatever reason after I get teleported my character just dies.

It is because in your code the

 if #players == 1 then

Statment is executing because you are the only player, if you had another player the

player.Character.Humanoid.Health = 0

Won’t execute, that is what I believe is happening

You can do a test with two clients and it should work I hope

While that does make sense as to why it wasn’t working before I have just tested it with 2 accounts and they still die for whatever reason. I think the reason is that there is no set amount of time for when a player can join the tournament or cannot but I’m not sure how to fix this issue.

That is weird, the only other thing I could think of is what you said, add some sort of timer that initiates when at least 2 players have pressed the yes, so that both join in at the same time.

Something like this:

if #players >= 2 then
   -- Add the Timer Logic here.
end

Try this, I added an additional if statment to make sure there are at least two players in the table

local players = {}
local tournamenton = false

while wait(10) do
	players = {} 

	for i, player in pairs(game.Players:GetPlayers()) do
		local c = ui:Clone()
		c.Parent = player.PlayerGui

		local bonksamountwin = math.random(100, 250)
		c.OuterFrame.Prize.Text = "A bonk challenge is starting with a prize of " .. bonksamountwin .. " bonks."

		c.OuterFrame.YES.MouseButton1Click:Connect(function()
			c:Destroy()
			tournamenton = true
			players = {player}
			player.Character:MoveTo(workspace.Tournament.tp.Position)

			player.Character.Humanoid.Died:Connect(function()
				local index = table.find(players, player)
				table.remove(players, index)
			end)
			if #players >= 2 then -- Added if statment before the loop.
				while wait(1) do
					if #players == 1 then
						local winner = script.WINNER:Clone()
						winner.WINNER.Visible = true
						winner.WINNER.Text = player.Name .. " HAS WON " .. bonksamountwin .. " BONKS!"
						player.Character.Humanoid.Health = 0
						player.leaderstats.Bonks.Value += bonksamountwin
						wait(2)
						winner:Destroy()
						tournamenton = false
						break
					end
				end
			end
		end)

		c.OuterFrame.NO.MouseButton1Click:Connect(function()
			c:Destroy()
		end)

		wait(7)
		c:Destroy()
	end
end

They don’t die immediately now but even when one player resets the “winner” GUI doesn’t come up.

Try this, I made a couple new functions and reworked the logic a bit, I think this version should work.

local players = {}
local tournamentOn = false

local ui = script.YourUICloningFunction()

while true do
    players = {}

    for _, player in pairs(game.Players:GetPlayers()) do
        local c = ui:Clone()
        c.Parent = player.PlayerGui

        local bonksAmountWin = math.random(100, 250)
        c.OuterFrame.Prize.Text = "A bonk challenge is starting with a prize of " .. bonksAmountWin .. " bonks."

        local function startTournament()
            c:Destroy()
            tournamentOn = true
            players = {player}
            player.Character:MoveTo(workspace.Tournament.tp.Position)

            local function onPlayerDied()
                local index = table.find(players, player)
                table.remove(players, index)

                if #players == 1 then
                    local winner = script.WINNER:Clone()
                    winner.WINNER.Visible = true
                    winner.WINNER.Text = players[1].Name .. " HAS WON " .. bonksAmountWin .. " BONKS!"
                    players[1].Character.Humanoid.Health = 0
                    players[1].leaderstats.Bonks.Value += bonksAmountWin
                    wait(2)
                    winner:Destroy()
                    tournamentOn = false
                end
            end

            player.Character.Humanoid.Died:Connect(onPlayerDied)
        end

        c.OuterFrame.YES.MouseButton1Click:Connect(function()
            if #players >= 2 then
                startTournament()
            else
                -- You can display a message or take some action when there are not enough players.
                print("Not enough players to start the tournament.")
            end
        end)

        c.OuterFrame.NO.MouseButton1Click:Connect(function()
            c:Destroy()
        end)

        wait(7)
        c:Destroy()
    end

    wait(10)
end
1 Like

image

When I click it it doesn’t tp me or my alt and I’ve also just realized that the GUI is appearing at different times for each player.