Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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)
0
You used Random.new() incorrectly that's why. JesseSong 3916 — 2y

3 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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!

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. CuzImArmy 7 — 2y
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 TheSuperDiamond77 30 — 2y
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) TheSuperDiamond77 30 — 2y
Ad
Log in to vote
0
Answered by
A_Mp5 222 Moderation Voter
2 years ago

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)
Log in to vote
0
Answered by
sngnn 274 Moderation Voter
2 years ago
Edited 2 years ago

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.

Answer this question