Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 07 Jul 2012 05:09 PM |
Intended Result: A user equips a tool and clicks a button. A fireball appears and flies towards the enemy. When it hits, it calculates an appropriate amount of damage and deals it to the enemy.
Actual result: The fireball hits the enemy, but no damage is taken.
Output: Workspace.Fire Wand.FireballScript:16: bad argument #1 to 'random' (number expected, got nil)
Relevant code sections:
-- start of script tool = script.Parent
-- inside function calculateDamage dealt = (baseDamage + math.random(stat/2, stat) - math.random(enemyStat)) * 1.5
-- passed values to the function local damage = 10 local character = tool.Parent lcoal stat = character.Magick.Value local enemy = hit.Parent local enemyStat = enemy.Magick.Value local advantage = calculateAdvantage(character.Type, enemy.Type) enemy.Humanoid:TakeDamage(calculateDamage(damage, stat, enemyStat, advantage))
What the output/code means: Either 'stat' or 'enemy' stat is not being recognized as valid. Both the object being hit and the fired object have it (there is error checking that I've left out. I can provide the full functions if requested). |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2012 05:14 PM |
Try: dealt = (baseDamage + math.random(stat/2, stat) - enemyStat) * 1.5
math.random(enemyStat) <- Only has one argument. Math random doesnt work with one argument. |
|
|
| Report Abuse |
|
|
WhiteRain
|
  |
| Joined: 24 Apr 2010 |
| Total Posts: 2723 |
|
|
| 07 Jul 2012 05:21 PM |
math.random can function with only argument. Ex: print(math.random(6)) Will print any number from 1-6 by default. If you have no arguments it will generate a real number between 0 and 1
|
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 07 Jul 2012 05:22 PM |
"math.random ([m [, n]]) This function returns a random integer between m and n. If only the first argument, m is passed, it returns an integer between 1 and m."
the wiki lied to me? I tried changing it to math.random(1, enemyStat) and got different output
Workspace.Player.Fire Wand.FireballScript:16: bad argument #2 to 'random' (number expected, got nil)
Seeing as it changed from 1 to 2, it is the enemyStat's math.random() that is the problem. Seeing as it "got nil," does this mean that the enemyStat isn't being found?
|
|
|
| Report Abuse |
|
|
WhiteRain
|
  |
| Joined: 24 Apr 2010 |
| Total Posts: 2723 |
|
|
| 07 Jul 2012 05:23 PM |
| Have you defined enemyStat? Because that seems to be the problem. |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 07 Jul 2012 05:26 PM |
This is the entire function where things are defined:
function blow(hit) print(hit.Name) damage = 10 if hit == nil then return end
local enemy = hit.Parent local enemyHum = enemy:findFirstChild("Humanoid") if enemyHum then local enemyStat = enemy.Magick.Value local enemyType = enemy.Type.Value end local character = tool.Parent local humV = character:FindFirstChild("Humanoid") local stat = character.Magick.Value local type = character.Type.Value
if enemyHum and character and (enemyHum ~= character) then advantage = findAdvantage(type, enemyType) enemyHum:TakeDamage(calculateDamage(damage, stat, enemyStat, advantage)) end end
enemyStat is defined here: if enemyHum then local enemyStat = enemy.Magick.Value local enemyType = enemy.Type.Value end |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 07 Jul 2012 05:44 PM |
Via print() to provide more detailed output, I have found the following out:
if enemyHum then local enemyStat = enemy.Magick.Value local enemyType = enemy.Type.Value end
This part does get the enemyStat. By adding print(enemyStat), the output shows "10"
However, by the time we get to here:
if enemyHum and character and (enemyHum ~= character) then
The value is lost. Is this because I defined it as local, and the scope goes out as soon as the if statement? Or is there some other reason that it loses the value? |
|
|
| Report Abuse |
|
|
WhiteRain
|
  |
| Joined: 24 Apr 2010 |
| Total Posts: 2723 |
|
|
| 07 Jul 2012 05:48 PM |
| Hmmm. May I see the calculateDamage() function? |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 07 Jul 2012 06:24 PM |
Sorry for the late reply:
function calculateDamage(baseDamage, stat, enemyStat, bool) if bool then dealt = (baseDamage + (math.random(stat/2 , stat) * 2) - math.random(1, enemyStat)) * 1.7 print("Bonus damage!") else dealt = (baseDamage + math.random(stat/2, stat) - math.random(1, enemyStat)) * 1.5 print("Normal damage.") end return dealt end |
|
|
| Report Abuse |
|
|
WhiteRain
|
  |
| Joined: 24 Apr 2010 |
| Total Posts: 2723 |
|
|
| 07 Jul 2012 06:31 PM |
Try defining enemyStat as this. local enemyStat = enemy.Magick
and then once you use it do enemyStat.Value |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 07 Jul 2012 06:35 PM |
It was just being weird with the if statement. I moved the declaration of enemyStat into the later if statement (before sending them to calculate damage) and it seems to work. However, the damage isn't actually being dealt. Is there something wrong with:
enemyHum:TakeDamage(calculateDamage(damage, stat, enemyStat, advantage))
Does it not count the damage being taken as the return value from the function? |
|
|
| Report Abuse |
|
|