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 4 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!

local chance = math.random()
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.F then
        if chance = .1 then
        print ('lol its a one')
    end
    if chance = .2 then
        print ('oof its a two')
    end
    if chance = .3 then
        print ('yeet its a three')
    end
    if chance = .4 then
        print ('what a beaner its a four')
    end
    if chance = .5 then
        print ('lol noob u got a five hahaha')
    end
    if chance = .6 then
        print ('f in chat you just won the game rolling a six!')
    end
end)

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
4 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!

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.F then
        local chance = math.random(1,6) -- So it can be a different number each time they press F
        if chance == 1 then
            print ('lol its a one')
        elseif chance == 2 then
            print ('oof its a two')
        elseif chance == 3 then
            print ('yeet its a three')
        elseif chance == 4 then
            print ('what a beaner its a four')
        elseif chance == 5 then
            print ('lol noob u got a five hahaha')
        elseif chance == 6 then
            print ('f in chat you just won the game rolling a six!')
        end
    end
end)

Ad
Log in to vote
0
Answered by
Rynappel 212 Moderation Voter
4 years ago
Edited 4 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

local chance = math.random(1,6)
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.F then
        if chance = 1 then
        print ('lol its a one')
    end
    if chance = 2 then
        print ('oof its a two')
    end
    if chance = 3 then
        print ('yeet its a three')
    end
    if chance = 4 then
        print ('what a beaner its a four')
    end
    if chance = 5 then
        print ('lol noob u got a five hahaha')
    end
    if chance = 6 then
        print ('f in chat you just won the game rolling a six!')
    end
end)
0
lol my english sucked there, just change the code to that, the error was on line 1 Rynappel 212 — 4y
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 — 4y
Log in to vote
0
Answered by 4 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.

local chance = math.random(1, 6)

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

 if 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:

 if chance == 1 then

Working Script:


local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(input, event) if input.KeyCode == Enum.KeyCode.F then local chance = math.random(1, 6) if chance == 1 then print ('lol its a one') end if chance == 2 then print ('oof its a two') end if chance == 3 then print ('yeet its a three') end if chance == 4 then print ('what a beaner its a four') end if chance == 5 then print ('lol noob u got a five hahaha') end if chance == 6 then print ('f in chat you just won the game rolling a six!') end end end)

Answer this question