Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I turn this script into an actually working one?

So this script actually doesn't randomize the value it just gives me the same all over again. And also how can I make it randomize with a percentage chance?

local example = {"Sung", "Hanma", "Yami"}

local randomNum = Random.new() :NextInteger(1, #example)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message == "!reroll" then
			print(example[randomNum])
		end
	end)
end)

1 comment

0

You used Random.new() incorrectly that's why.

3 answers

✓ Accepted answer

Hey lovely developer! This is how i would make it!

local example = {"Sung", "Hanma", "Yami"}
local randomNum = 0

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message == "!reroll" then
			randomNum = math.random(1,#example)
          		print(example[randomNum])
		end
	end)
end)

#example give you the length of the table! I think doing math.random() is better!

https://developer.roblox.com/en-us/api-reference/lua-docs/math

it will just choose a number between [1,2,3]!

Have a nice day!

3 comments

0

Yeah that seemed to have fixed it but do you have any idea on how to make it work of percentages? Like 1 value in the table is harder to get than another.

0

Hi CuzImArmy, to achieve this what you can do it randomNum = math.Random(1,#example)/10 * 100. To get a pourcentage, the value must be between 0 and 1. To fix this we do [RANDOM NUMBER] / (this means divide) 10. So for exemple when the random number is 3 the value after this operation will be 0,3. Then after that we multiply 0,3 (by using this *) by 100 to give us a pourcentage! Here ; 3/10 = 0,3

0

Oh wait my bad i missed understood the question, writting this would be way to long but you can achieve this by using the "Weighted Selection". You can find this in the Egg hatching system tutorial by Alvinblox (12:53)

You assigned the variable to run the function once, and then use it upon playeradded.

local example = {"Sung", "Hanma", "Yami"}

local randomNum -- setting variable

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if message == "!reroll" then
		randomNum = math.Random(1,#example) --Randomizes the variable every time its ran
            print(example[randomNum])
        end
    end)
end)

You aren't changing the random number at all. Simply repositioning the line should fix it.

local example = {"Sung", "Hanma", "Yami"}
local randomNum = Random.new() 

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if message == "!reroll" then
            print(example[randomNum:NextInteger(1, #example)])
        end
    end)
end)

Edit: Seems like this question was already answered.

This discussion is preserved as it was. New answers, comments, and votes are closed.