• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

TrinityCore WotLK Math.Random - How it works / Question

Sixon

Emulation Addict
Hi guys,

Just wondering if anyone can give me some insight.
What I'm trying to do is if a player has X quest In Y zone they'll get 2 debuffs random debuffs from a list of debuffs

Code:
function Quest(event, player, newZone, newArea) 

if(player:getzoneid() == Y) then -- Might need to do AreaID here not sure yet
    if(player:hasQuest(X) == true then 
		local chance = math.random(1,8)

			if(chance == 1) then
			Player:CastSpell(DebuffSpellEnrty))

			elseif(chance == 2) then
			Player:CastSpell(DebuffSpellEnrty)
			
			elseif(chance == 3) then
			Player:CastSpell(DebuffSpellEnrty)
			
			elseif(chance == 4) then
			Player:CastSpell(DebuffSpellEnrty)
			
			elseif(chance == 5) then
			Player:CastSpell(DebuffSpellEnrty)
			
			elseif(chance == 6) then
			Player:CastSpell(DebuffSpellEnrty)
			
			elseif(chance == 7) then
			Player:CastSpell(DebuffSpellEnrty)

			elseif(chance == 8) then
			Player:CastSpell(DebuffSpellEnrty)
                 
            end
    else 
        Player:RemoveSpell(DebuffSpellEnrty)
    end
else 
    if(player:haspell(DebuffSpellEnrty) == true) then 
        Player:RemoveSpell(DebuffSpellEnrty) 
    end
end

end

Is it as simple as changing
Code:
local chance = math.random([COLOR="#FF0000"]2[/COLOR],8)
I don't know if I'm completely wrong with this but Will I have to change the
Code:
if(chance == 1) then
			Player:CastSpell(DebuffSpellEnrty))
Bits to be more like

Code:
if(chance == 1 or chance == 2) then
	Player:CastSpell(DebuffSpell1)
        Player:CastSpell(DebuffSpell2))

And do this for all of them all combinations.
Any help will be much appreciated or if you know of any forums / Wikis with the explanations on how it works.
 

Rochet2

Moderator / Eluna Dev
Assuming you want two different debuffs from a list:

Code:
local debuffs = {1,2,3} -- assumed to have 2 or more items in it

-- select 2 random debuff from the list
local debuff1 = math.random(1, #debuffs)
local debuff2 = math.random(1, #debuffs-1) -- notice that we select with one smaller range as one is already selected
-- if second debuff is higher or equal to first debuff index, then we must increase it by one to get the correct index
if debuff1 <= debuff2 then
    debuff2 = debuff2+1 -- safe because we know debuff2 is between 1 and size-1
end
player:CastSpell(player, debuffs[debuff1], true)
player:CastSpell(player, debuffs[debuff2], true)

if you want two debuffs that can be the same, then you can just do two math.random(1, #debuffs) calls and use the returned values by each call as index to the debuffs array.

Read http://www.lua.org/manual/5.2/manual.html#pdf-math.random
 
Last edited:
Top