So the code below checks to see if a random value is between a specific number by using if statements, since I’ve been using quite a lot of if statements for many events of my game to be called out, I wanna know if there’s a shorter way to do so, instead of using if statements. If you want to suggest what you want me to try out, please let me know. Thanks!
local random = math.random(0,5000)
if random > 0 and random < 2500 then
for i,tile in pairs(Tiles:GetChildren()) do
if i >=2 then
Tiles:GetChildren()[2].Size = Tiles:GetChildren()[2].Size + Vector3.new(5,0,5)
Status.Value = Tiles:GetChildren()[2].Name.. "'s Plate grown by 5 studs"
end
end
end
if random > 500 and random < 1000 then
tileEvent(Tiles:GetChildren())
end
if random > 1000 and random < 2000 then
local WindPart = game.ServerStorage.WindPart:Clone()
WindPart.Parent = workspace
windEvent(WindPart:GetChildren())
Status.Value = "Wind appears"
end
if random > 2000 and random < 3000 then
local eventFolder = game.ServerStorage.RegionPart:Clone()
eventFolder.Parent = workspace
quickSandEvent(Tiles:GetChildren(),eventFolder)
end
if random > 3000 and random < 4000 then
local laserFolder = game.ServerStorage.LaserFolder:Clone()
laserFolder.Parent = workspace
end
if random > 4000 and random < 5000 then
fireEvent(Tiles:GetChildren())
end
The following is not a perfect script, but it works and you can further improve it.
Explanation
First of all, you create an array storing possible outcomes in pairs (it is sort of a dictionary). GetIndex() is only a trigger function, which we call to get random number and which calls other functions.
Next step is comparing of the values.
Looping through the table, we are trying to find the pair given random number can fit among.
Using returned index you can determine which further function is to be call (I named the manager function “MethodManager”, although these are not actually methods.
local PossibleOutcomes = {
{1, 3};
{3, 5};
{5, 7};
}
local function MethodManager(index)
if index == nil then return; end -- doesn't exist if for example random number equals 8
if index == 1 then "call function here" ; end
if index == 2 then "call another function here" ; end
end
local function CompareValues(random)
for index, value in pairs(PossibleOutcomes) do
if random > PossibleOutcomes[index][1] and random < PossibleOutcomes[index][2] then
return index
end
end
end
local function GetIndex()
local random = math.random(1, 7)
local index = CompareValues(random)
MethodManager(index)
end
Addition:
And here is your specific case (I would rather use module scripts if I were you):
PossibleOutcomes = {
{1000, 2000};
}
local function Why_not_make_it_a_little_windy()
local WindPart = game.ServerStorage:WaitForChild("WindPart"):Clone()
WindPart.Parent = workspace
windEvent(WindPart:GetChildren())
Status.Value = "Wind appears"
end
local function MethodManager(index)
if index == nil then return; end
if index == 1 then Why_not_make_it_a_little_windy()
end