Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

if - then wont work?

Asked by 9 years ago

I'm working on an AI for a turn-based RPG enemy. Currently, it's in very early alpha, and I seem to have run into a problem. Take a look:

01local trigger = script.Parent.canattack
02local oh = script.Parent.Values.HP
03local sh = script.Parent.Values.vHP
04 
05trigger.Changed:connect(function()
06    if script.Parent.canattack.Value == true then
07        if oh.Value < sh.Value / 2 then
08            game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." used a health potion!"
09            wait(2)
10            game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." Has healed 25 health!"
11            wait(2)
12            oh.Value = oh.Value + 25
13            if oh.Value > sh.Value then
14                local rh = oh.Value - sh.Value
15                oh.Value = oh.Value - rh
View all 32 lines...

My problem is, once the game senses that "canattack" has changed, it won't decide what to do next. Its a little complex, I'll explain. So basically, once the players turn is done, it sets the AI's "canattack" Bool value to True. The game senses its change to True, and calculates what attack the enemy should do based on its Health. For just testing purposes, I have the enemy choose between 2 options: If its current health is higher than half of its original health, then it attacks. If its current health is less than half its original health, then it heals itself. The problem comes that the script won't decide between the two. When I noticed that nothing was happening when I fought the AI, I put prints in both of the options, and neither one fired. Now, the "canattack" If Then statement works, as I tested it with a print also (the print fired). The problem is that once it gets to reading the AIs health and determining the attack it should do, it stops.

KEY: trigger = canattack Bool Value oh = The AIs current health (changes as he heals, takes damage, etc.) sh = The AIs original health (Its full/max health, the health it spawned with. Used to calculate damage taken).

1 answer

Log in to vote
0
Answered by 9 years ago
01local trigger = script.Parent.canattack
02local oh = script.Parent.Values.HP
03local sh = script.Parent.Values.vHP --original hp
04 
05trigger.Changed:connect(function()
06    if script.Parent.canattack.Value == true then
07        if oh.Value < sh.Value / 2 then
08            game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." used a health potion!"
09            wait(2)
10            game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." Has healed 25 health!"
11            wait(2)
12            oh.Value = oh.Value + 25
13            if oh.Value > sh.Value then
14                local rh = oh.Value - sh.Value
15                oh.Value = oh.Value - rh
View all 32 lines...

Another thing I noticed is that you said "half of its original health". Currently you have it set up like this.

01"Stats: (I put it like this so it's formatting is neater)"
02"Total Health 100"
03"Previous(original) Health 60"
04"Current health 40"
05 
06"Base off of this the AI would choose to attack rather then health because"
07"original health/2 (60/2) is greater then current health(40)"
08"If you wanted it to be based on total health, where in this scenario the AI would heal at any health under 50, change"
09sh.Value/2
10"to"
11totalhealth/2 "(you'll have to make this a variable.)"

P.S. Seems like a cool game! I'd love it if you would pm me when it goes into beta! I could also answer more questions for you!

Ad

Answer this question