if - then wont work?
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:
01 | local trigger = script.Parent.canattack |
02 | local oh = script.Parent.Values.HP |
03 | local sh = script.Parent.Values.vHP |
05 | trigger.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!" |
10 | game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = "" ..script.Parent.Name.. " Has healed 25 health!" |
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 |
17 | game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = "" ..script.Parent.Name.. " has " ..oh.. " health!" |
19 | elseif oh.Value > sh.Value / 2 then |
20 | local oph = game.Players.LocalPlayer.PlayerGui.Values.vHP.Value |
21 | local ph = game.Players.LocalPlayer.PlayerGui.Values.HP.Value |
22 | game.Players.LocalPlayer.PlayerGui.Values.HP.Value = game.Players.LocalPlayer.PlayerGui.Values.HP.Value - script.Parent.Values.Damage.Value |
23 | game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = "" ..script.Parent.Name.. " attacked you!" |
25 | game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = "You took " ..oph - ph.. " damage!" |
29 | oh.Changed:connect( function () |
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).