I want to make a script that gives a player a random assigned number when they join the game

I have started the script but don’t have enough scripting skills to make my own script from scratch. I need to write some code to get a random assigned number for each player that joins the game and after I need to figure out how to put that number on the player leaderboard so they can see it.

I’m trying to use a leaderboard to display the number a player gets randomly when they join the game.

(this number will determine the amount of lives they have in the game)

image

Here’s the code for assigning a number to a player once they join the game and the start of the code for the leaderboard. (I don’t know exactly how to get a random number to show up on the leaderboard, but I want to learn)

game.players.PlayerAdded:Connect(function(players)
	local leaderboard = instance.new("BoolValue", players)
	leaderboard.Name = "leaderstats"
	local Lives = Instance.new("IntValue", leaderboard)
	Lives.Name = "Lives"
	Lives.Value = print(math.random(10,100)
	end
end)

I looked for this script on devforum but couldn’t find anyone asking the same question.

It would help a lot so I could make my game’s scripts if someone would show me how to make my script display a random number when a player joins.

Thank you for reading.

Your code should work fine. You just forgot to make the first letter of instance.new capital on the second line. Try this:

game.Players.PlayerAdded:Connect(function(players)
	local leaderboard = Instance.new("BoolValue", players)
	leaderboard.Name = "leaderstats"
	local Lives = Instance.new("IntValue", leaderboard)
	Lives.Name = "Lives"
	Lives.Value = math.random(10,100)
	end
end)

To assign random numbers, math.random is used which gives a random number between the two numbers given. Also, keep in mind that Lives.Value = print(math.random(10,100) will not work because you are trying to print and set the value at the same time. Instead you can set the value and then print like so:

Lives.Value = math.random(10,100)
print(Lives.Value)

More about math library: math
Also, I recommend you to watch some tutorials on youtube before writing scripts.
Try these:
https://www.youtube.com/playlist?list=PLhieaQmOk7nIfMZ1UmvKGPrwuwQVwAvFa

Thank you for responding, I had already caught that bug at the second line when I took the screenshot. However, thank you so much for correcting my mistake of setting the value and printing at the same time. I didn’t know before that it wasn’t possible to do it at the same time. The leaderboard and life counter now work perfectly.

image

1 Like

Make sure to mark the topic as solved if your problem is solved.

You have an unnecessary end statement at the end of the code. It’s also worth mentioning that the print global function returns 0 values (not even ‘nil’), this can be determined via the following code.

print(select("#", print("Hello world!"))) --0