Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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:

local trigger = script.Parent.canattack
local oh = script.Parent.Values.HP
local sh = script.Parent.Values.vHP

trigger.Changed:connect(function()
	if script.Parent.canattack.Value == true then
		if oh.Value < sh.Value / 2 then
			game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." used a health potion!"
			wait(2)
			game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." Has healed 25 health!"
			wait(2)
			oh.Value = oh.Value + 25
			if oh.Value > sh.Value then
				local rh = oh.Value - sh.Value
				oh.Value = oh.Value - rh
			end
			game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." has "..oh.." health!"
		end
	elseif oh.Value > sh.Value / 2 then
		local oph = game.Players.LocalPlayer.PlayerGui.Values.vHP.Value
		local ph = game.Players.LocalPlayer.PlayerGui.Values.HP.Value
		game.Players.LocalPlayer.PlayerGui.Values.HP.Value = game.Players.LocalPlayer.PlayerGui.Values.HP.Value - script.Parent.Values.Damage.Value
		game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." attacked you!"
		wait(2)
		game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = "You took "..oph - ph.." damage!"
	end
end)

oh.Changed:connect(function()
	print(oh.Value)
	print(sh.Value)
end)

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

local trigger = script.Parent.canattack
local oh = script.Parent.Values.HP
local sh = script.Parent.Values.vHP --original hp

trigger.Changed:connect(function()
    if script.Parent.canattack.Value == true then
        if oh.Value < sh.Value / 2 then
            game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." used a health potion!"
            wait(2)
            game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." Has healed 25 health!"
            wait(2)
            oh.Value = oh.Value + 25
            if oh.Value > sh.Value then
                local rh = oh.Value - sh.Value
                oh.Value = oh.Value - rh
            end
            game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." has "..oh.." health!"
	    elseif oh.Value > sh.Value / 2 then
	        local oph = game.Players.LocalPlayer.PlayerGui.Values.vHP.Value
	        local ph = game.Players.LocalPlayer.PlayerGui.Values.HP.Value
	        game.Players.LocalPlayer.PlayerGui.Values.HP.Value = game.Players.LocalPlayer.PlayerGui.Values.HP.Value - script.Parent.Values.Damage.Value
	        game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = ""..script.Parent.Name.." attacked you!"
	        wait(2)
	        game.Players.LocalPlayer.PlayerGui.HUD.CombatPanel.TextLabel.Text = "You took "..oph - ph.." damage!"
		end   
 	end
end)

oh.Changed:connect(function()
    print(oh.Value)
    print(sh.Value)
end)

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

"Stats: (I put it like this so it's formatting is neater)"
"Total Health 100"
"Previous(original) Health 60"
"Current health 40"

"Base off of this the AI would choose to attack rather then health because"
"original health/2 (60/2) is greater then current health(40)"
"If you wanted it to be based on total health, where in this scenario the AI would heal at any health under 50, change"
sh.Value/2
"to"
totalhealth/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!

This discussion is preserved as it was. New answers, comments, and votes are closed.