Issues with a rap battle script

Hi, I’m trying to develop a rap battle game. I don’t see why, but this script doesn’t run when it is enabled, it is in serverscriptservice and it’s setup perfectly. Here’s the code:

local players = game:GetService("Players")
local Players = players:GetPlayers()
local stage = game.Workspace.Stage


while true do
	repeat wait() until #Players ~= 1
    game.ReplicatedStorage.Introduce:FireAllClients()
	wait(5)
	local randomPlayer1 = Players[math.random(1,#game.Players:GetPlayers())]
	local randomPlayer2 = Players[math.random(1,#game.Players:GetPlayers())]
	repeat
		randomPlayer2 = Players[math.random(1,#game.Players:GetPlayers())]
	until (randomPlayer2 ~= randomPlayer1)
	print("Random player found")
	for i,v in pairs(Players) do
		v.PlayerGui.Status.Users.Text = randomPlayer1.Name.." vs "..randomPlayer2.Name
	end
	print("Successfully started round")
	local randomUserId2 = randomPlayer2.UserId
	local randomUserId = randomPlayer1.UserId
	local content1 = players:GetUserThumbnailAsync(randomUserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
	local content2 = players:GetUserThumbnailAsync(randomUserId2,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
	randomPlayer1:SetAttribute("IsPlayer",true)
	randomPlayer2:SetAttribute("IsPlayer",false)
	local randomPlayerName = randomPlayer1.Name
	local randomPlayerName2 = randomPlayer2.Name    
	wait(5)
	for i,v in pairs(Players) do
		v.PlayerGui.Status.Users.Text=""
		v.PlayerGui.Status.Sub.Text = "Alright, get ready to throw down!"
		wait(1)
		v.PlayerGui.Status.Users.Text = "You're up first, "..randomPlayerName
	end
	wait(2)
	for i,v in pairs(Players) do
		v.PlayerGui.Subtitles.Rapper.Visible = true
	end
	print("Round started: Players are "..randomPlayerName.."and"..randomPlayerName2)
	randomPlayer1.Chatted:Connect(function(msg)
		if randomPlayer1:GetAttribute("IsPlayer",true) then
			for i,v in pairs(Players) do
				v.PlayerGui.Subtitles.Rapper.message.Text = msg
			end
		end
	end)
	randomPlayer2.Chatted:Connect(function(msg)
		if randomPlayer2:GetAttribute("IsPlayer",true) then
			for i,v in pairs(Players) do
				v.PlayerGui.Subtitles.Rapper.message.Text = msg
			end
		end
	end)
	for i, v in pairs(Players) do
		for i = 40, 0, -1 do
			v.PlayerGui.Status.Sub.Text = tostring(i)
		end
	end	
	for i, v in pairs(Players) do
		v.PlayerGui.Status.Sub.Text = "Dang homie, how are you going to come back from that?"
	end	
	randomPlayer1:SetAttribute("IsPlayer",false)
	wait(5)
	for i, v in pairs(Players) do
		v.PlayerGui.Status.Sub.Text = randomPlayerName2..", you're up next!"
	end	
	wait(5)
	randomPlayer2:SetAttribute("IsPlayer",true)
	randomPlayer2.Chatted:Connect(function(msg)
		if randomPlayer2:GetAttribute("IsPlayer",true) then
			for i,v in pairs(Players) do
				v.PlayerGui.Subtitles.Rapper.message.Text = msg
			end
		end
	end)
	for i, v in pairs(Players) do
		for i = 40, 0, -1 do
			v.PlayerGui.Status.Sub.Text = tostring(i)
		end
	end	
	game.ReplicatedStorage.Voting:FireAllClients()
end

I ran this on a local server with 2 players and yet still didn’t run and had no errors.

4 Likes

My only suggestion to try would be to change the

repeat wait() until #Players ~= 1

to

repeat task.wait() until #Players >= 2

because my only though when reading this because I can’t test it out myself right now is that it runs before a player can be added because it is only checking to make sure that the amount of players is not one, but it will run if there is any other number of players.

But if that doesn’t work, try printing some of the values and stuff.
Tell me if this helps a little.

2 Likes

Still ran it and it somehow doesn’t work. I also added a print statement which worked in which my only assumption would be an issue with the next thread, but I still don’t know what it is.

1 Like

While this isn’t a solution for your problem, I’m noticing that you’re creating Chatted connections within the while loop and you’re not disconnecting them. If your script does run this will cause a memory leak so be careful about creating connections within loops

1 Like

Yes but I had to include them in the while loop since the variables were set there - if I had the variables outside the while loop it would keep the same players everytime.

can you put a print statement inside of this loop and also add a wait, else your script exhausts

1 Like

Ok, but it would only exhaust if it were to keep the same random player pairs 10000 times which is unlikely, but yes I’ve added a print statement but the thread halted before that specific repeat loop. Also I added a wait just after I edited the first post.

is this good enough?

Still this will cause problems in the future unfortunately so I suggest you research about memory leaks when you have the time

1 Like

Would once only fire the message one time? What if the player has to chat more than once or would this last for the while loop? (if I’m dumb lol)

1 Like

You should probably put the repeat loops inside of an if statement

if #Players <= 1 then
	repeat wait() until #Players >= 2
end
-- code
if randomPlayer2 == randomPlayer1 then
	repeat
		randomPlayer2 = Players[math.random(1,#game.Players:GetPlayers())]
	until (randomPlayer2 ~= randomPlayer1)
end

Last Option:
Put a print statement at the top of the while loop and script, check if it prints anything

My bad, that was a bad suggestion so I removed it

1 Like

Yes I’ve added a print statement after every thread, but it somehow only got to the repeat until loop as if there were an issue with the #players in which however I don’t see anything in the output

Edit: I think it might have been an issue with the repeat loop since both clients in the game do not load at the same time

1 Like

I think that your script might not be running. Put a print at the top of the script. And question what type of script is your script?

1 Like

It’s a regular script in serverscriptservice, and as I stated earlier it did print messages into the output.

repeat task.wait() until #Players ~= 1

Doesn’t work, as Players is a local, and players:GetPlayers() only returns the amount of players in game at the time you call it, and doesnt add any new players to the table it returns when a player is added. so you need to do

repeat task.wait() until #players:GetPlayers() ~= 1
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.