Still have questions? Join our Discord server and get real time help.
0

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

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.

1
Shawnyg 4241
5 months 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)


0
Rynappel 209
5 months ago
Edited 5 months 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 209 — 5mo
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 — 5mo
0

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)