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.
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.
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.
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
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.
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.
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
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
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