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

I'm experimenting with lua when I decide to try to make a dice roll system. Why no worky?

Asked by 5 years ago

I finished the beginning of it and too see if it works, I press play and press f, which is the input i've chosen to change the math, and nothing pops up in the output. I'm somewhat of a beginner, so please give some feedback ill understand!

01local chance = math.random()
02local UserInputService = game:GetService("UserInputService")
03UserInputService.InputBegan:Connect(function(input, event)
04    if input.KeyCode == Enum.KeyCode.F then
05        if chance = .1 then
06        print ('lol its a one')
07    end
08    if chance = .2 then
09        print ('oof its a two')
10    end
11    if chance = .3 then
12        print ('yeet its a three')
13    end
14    if chance = .4 then
15        print ('what a beaner its a four')
View all 23 lines...

yes i understand it goes through 0-1 but i don't think thats the problem here.

3 answers

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
5 years ago

You made a great attempt on your code. To point out some issues with it, math.random() returns a decimal number between 0 and 1. It will most likely never be a clean rational 0.1, but more so a 0.1214101241. Instead, you could set your bounds to be between 1 and 6, and in doing so it will only choose whole numbers. For your if statements, you'd want to use elseif's since you're comparing one outcome, which is the outcome of your die (singular for dice). Lastly, the comparison operator is ==, where you only had =. One equal sign is for an expression, to define something. Any questions, just comment!

01local UserInputService = game:GetService("UserInputService")
02UserInputService.InputBegan:Connect(function(input, event)
03    if input.KeyCode == Enum.KeyCode.F then
04        local chance = math.random(1,6) -- So it can be a different number each time they press F
05        if chance == 1 then
06            print ('lol its a one')
07        elseif chance == 2 then
08            print ('oof its a two')
09        elseif chance == 3 then
10            print ('yeet its a three')
11        elseif chance == 4 then
12            print ('what a beaner its a four')
13        elseif chance == 5 then
14            print ('lol noob u got a five hahaha')
15        elseif chance == 6 then
16            print ('f in chat you just won the game rolling a six!')
17        end
18    end
19end)
Ad
Log in to vote
0
Answered by
Rynappel 212 Moderation Voter
5 years ago
Edited 5 years ago

The error was line 1, its hard to explain but u have to put the numbers inbetween the math.random brackets thing. Try this script

01local chance = math.random(1,6)
02local UserInputService = game:GetService("UserInputService")
03UserInputService.InputBegan:Connect(function(input, event)
04    if input.KeyCode == Enum.KeyCode.F then
05        if chance = 1 then
06        print ('lol its a one')
07    end
08    if chance = 2 then
09        print ('oof its a two')
10    end
11    if chance = 3 then
12        print ('yeet its a three')
13    end
14    if chance = 4 then
15        print ('what a beaner its a four')
View all 23 lines...
0
lol my english sucked there, just change the code to that, the error was on line 1 Rynappel 212 — 5y
0
That wont work as the | if chance = 3 then | needs to be == not =. Also the random will only work once and not change. BluesteelKd 2 — 5y
Log in to vote
0
Answered by 5 years ago

Pretty close but there is a few errors.

math.random needs a set of numbers in it otherwise it will automatically pick a number from 0-1 and can be unreliable.

1local chance = math.random(1, 6)

This will make the math.random choose a number from one to six.

1if chance = 1 then

When trying to check a number or string from a variable use == this will check if chance is the same number as 1. Using one equals will be telling the script to change the chance to 1 but it wont work.

Example:

1if chance == 1 then

Working Script:

01local UserInputService = game:GetService("UserInputService")
02 
03UserInputService.InputBegan:Connect(function(input, event)
04    if input.KeyCode == Enum.KeyCode.F then
05 
06local chance = math.random(1, 6)
07 
08    if chance == 1 then
09        print ('lol its a one')
10    end
11 
12    if chance == 2 then
13        print ('oof its a two')
14    end
15 
View all 33 lines...

Answer this question