Hello from Romania, is there a way to shorten a script?
local Dice = math.random(1,3) hint.Value = '3' wait(0.9) hint.Value = '2' wait(0.9) hint.Value = '1' wait(0.9) if Dice == 1 then P.CanCollide = false end if Dice == 2 then P.CanCollide = false end if Dice == 3 then P.CanCollide = false end end
Now, let say i want to do that to happen 6 more times, i could copy & paste,right? ,but let say im too lazy, what then? is there a way to shorten this out? i tryed to do:
Local Game1 = local Dice = math.random(1,3) hint.Value = '3' wait(0.9) hint.Value = '2' wait(0.9) hint.Value = '1' wait(0.9) if Dice == 1 then P.CanCollide = false end
Basicaly, i tryed to make a word mean a entire script. but it doesent work, it wold be cool tought. :D :D :D
It really depends on what you're trying to accomplish. If its all going to be CanCollide = false then you could check for a range of numbers or enter all acceptable numbers into a table and check that if they're scattered around. (1) Otherwise I would have a table with a function for each number, which would make it easier to read, as you wouldn't write if Dice == __ every time. (2) examples shown below for each
ex1.
Dice = math.random(1,3) --first nums = {1,3} if table.find(nums, Dice) then CanCollide = false end --second if Dice >=1 and Dice <=3 then CanCollide = false end
ex2.
Dice = math.random(1,3) -- gets you a random number 1-3 local functions = { [1] = function(P) -- example of changing cancollide on P specifically P.CanCollide = false end, [2] = function() -- example of another function print("landed on a 2, nothing happens in this example except this print") end, } if functions[Dice] then -- checks if the result from the dice is inside the table -- in this case, if it lands on a 3, nothing happens because i only put [1] and [2] in the table. functions[Dice]() -- runs function for rolled value, should work like any other function -- if you want to change P's property may want to use functions[Dice](P), in top example if you look inside table end
there are probably better ways but these are the two that i could think of at the time that to me would be easier to type.
local Dice for i = 1, 7, 1 do Dice = math.random(1,3) for i = 3, 1, -1 do hint.Value = i task.wait(0.9) end if Dice == 1 then P.CanCollide = false elseif Dice == 2 then P.CanCollide = false elseif Dice == 3 then P.CanCollide = false end end