Hello. I am making a betting gui that is supposed to be usable by all players. I don't know if this is correct. So basically, it makes a textbutton and plus the bet value by 1 when you click it. There is a timer too (30 second timer to bet). I kinda got stuck because it randomly picks a player and then everyone that bet on that player would get their coins doubled. Here is the script,
while wait(600) do -- wait 10 minutes before it appears again script.Parent.Frame.Visible = true for i, v in pairs(game.Players:GetPlayers()) do -- gets all players local button = Instance.new("TextButton", script.Parent.Frame) -- sets a bunch of stuff like making textbuttons for every person button.Text = v.Name button.BackgroundTransparency = 0.75 button.BackgroundColor3 = Color3.new(Vector3.new(0,0,0)) local betsLabel = Instance.new("TextLabel", button) local bets = Instance.new("IntValue", betsLabel) betsLabel.Text = bets.Value betsLabel.BackgroundTransparency = 1 betsLabel.TextColor3 = Color3.new(Vector3.new(255,255,255)) v.MouseButton1Click:connect(function() bets.Value = bets.Value + 1 end) for i = 30, 0, -1 do -- timer local timeL = Instance.new("TextLabel", script.Parent.Frame) timeL.Text = "Time Left: ".. i if i == 0 then -- when timer is done it randomly picks someone math.random() -- got stuck here end end end end
The parts im concerned about is the randomizing part and the places where it creates the textlabels and such.
The problem wasn't only the part about the math.random. There were also small mistakes.
The problem about the math.random was that you need to find a way to get a random player using math.random. Like how Cawlonee said, go to this site: math.random(). #game.Players:GetChildren()
returns the amount of players in-game. It's the same as game.Players.NumPlayers
. When math.random()
is left blank, it returns a "1" or a "0". Anyway, after it chooses a random number, It chooses a random player. If it returns a 10, it would take the 10th player in alphabetical order.
Another problem was the for loop. If you look on the wiki page here, you can see there is no, if i == 0 then
. They end with the keyword end
. You see, if for loops
have 3 different numbers, the loop keeps looping until the i
is equal to the 2nd number in the chain of those 3 numbers.
The last problem was that I forgot to delete the "ends" at the end. So if there is red lines under the "ends".
Also, There is one more error with your script near the for loop
part.
I also added the, "if winner.ClassName == "Player" then" to check if the "winner" or "chosen player" is an actual player.
while wait(600) do -- wait 10 minutes before it appears again local checker = Instance.new("StringValue") script.Parent.Frame.Visible = true for i, v in pairs(game.Players:GetPlayers()) do -- gets all players local button = Instance.new("TextButton", script.Parent.Frame) button.Text = v.Name button.BackgroundTransparency = 0.75 button.BackgroundColor3 = Color3.new(Vector3.new(0,0,0)) local betsLabel = Instance.new("TextLabel", button) local bets = Instance.new("IntValue", betsLabel) betsLabel.Text = bets.Value betsLabel.BackgroundTransparency = 1 betsLabel.TextColor3 = Color3.new(Vector3.new(255,255,255)) v.MouseButton1Click:connect(function() bets.Value = bets.Value + 1 checker.Name = [[I don't know how you would do this, but if someone clicks on the person to bet on them, make the checker name be that persons name.]] --EDIT THIS PART. checker.Parent = v end) for i = 30, 0, -1 do -- timer local timeL = Instance.new("TextLabel", script.Parent.Frame) timeL.Text = "Time Left: ".. i wait(1) --Add this so the timer doesn't go down immediately. local winner = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())] if winner.ClassName == "Player" then local checkforchecker = game.Players:GetChildren() for i = 1, #checkforchecker do if checkforchecker[i].ClassName == "Player" then local bet = checkforchecker[i]:GetChildren() for i = 1, #bet do if bet[i]:IsA("Instance") then if bet[i].Name = winner.Name then bet[i].Parent.Win.Value = bet[i].Parent.Win.Value --EDIT THIS PART end end end --Rest of the script. end end end end end
I'm not sure exactly what you wanted, but I'll happily explain how math.random works.
local number = math.random(MinNumber,MaxNumer)
It takes two numbers and returns a random integer between them, like so.
print(math.random(1,10)) -- 10 print(math.random(1,10)) -- 2 print(math.random(1,10)) -- 7
You can get a random player like this:
randomPlayer = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())]
It's creating a table of random players, and indexing it with a random number between 1 and the total number of players.