Else/elseifs being ignored and running even when it is false

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a game mode with 5 separate buildings, when one of them has the highest amount of health, they win.
  2. What is the issue? Include screenshots / videos if possible!
    I am trying to use else/elseifs to find out who has the most health, and if there is at least 2 with the same hp, it ties, but it ignores the else and elseifs and runs the first “if” regardless of it being true or not
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i have tried looking for solutions on the dev hub but none of the solutions work for me
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
    (im not the best at scripting so this code is gonna be a big eye sore)
ut = workspace.Map["Undertale Area"]["undertale hitbox"].Humanoid.Health
uf = workspace.Map["Underfell Area"]["underfell hitbox"].Humanoid.Health
ot = workspace.Map["Outertale Area"]["outertale hitbox"].Humanoid.Health
ht = workspace.Map["Horrortale Area"]["horrortale hitbox"].Humanoid.Health
us = workspace.Map["Underswap Area"]["swaptale hitbox"].Humanoid.Health
local numberTable = {uf, ut, ot, ht, us}
local hint = Instance.new"Hint"
local highestnumber = 0

for i, v in pairs(numberTable) do
	local number = tonumber(v)
	if number > highestnumber then
		highestnumber = number
	end
end
if highestnumber == ut then
	hint.Parent = workspace
	hint.Text = "undertale wins!"
	wait(5)
	hint:Destroy()

elseif highestnumber == uf then
	hint.Parent = workspace
	hint.Text = "underfell wins!"
	wait(5)
	hint:Destroy()

elseif highestnumber == ot then
	hint.Parent = workspace
	hint.Text = "outertale wins!"
	wait(5)
	hint:Destroy()

elseif highestnumber == ht then
	hint.Parent = workspace
	hint.Text = "horrortale wins!"
	wait(5)
	hint:Destroy()

elseif highestnumber == us then
	hint.Parent = workspace
	hint.Text = "underswap wins!"
	wait(5)
	hint:Destroy()
else
	hint.Parent = workspace
	hint.Text = "Tie!"
	wait(5)
	hint:Destroy()
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

For one thing, you should probably avoid using Hint since it was deprecated quite a while ago and doesn’t work.

Right now I’m just using it as a bases untill i get around to doing guis (this game is in its early early alpha stages)

Correct me if I’m wrong but, I believe Hints don’t function at all.

Start by sorting your array of healths! This will immediately get you the highest number and if the runner up is the same, then we have a tie! Here is some greater pseudo code you can use to get started.

table.sort(numberTable)

if numberTable[1] == numberTable[2] then
   print("Tie!")
else
    if numberTable[1] == ut then
        -- etc ...
    end
end

they are in fact working for me

Oh. Mind showing me what they look like?

image

1 Like

just tried it and it did the same thing
image

just realized that i had some useless code in it let me get rid of it

It didn’t do the same thing, it printed “Tie!”, also this is clearly incorrect code. Use elseif without a space and use roblox’s Format Document to fix the indentation.

i’m sorry I didn’t clarify, first it printed the first thing on the list, before, the first thing was “undertale”, now the first thing on it is “tie!”. and also as i have clearly stated, I’m fairly new to coding. I’m not too familiar with all the steps involved

Do you have Studio to API Access enabled in Game Settings?

no, was this just as simple as that?

I am not sure which way the sort sorts 1, 2, 3, 4 or 4, 3, 2, 1. print out the sorted table to see and if it’s 1, 2, 3, 4 (aka ascending) then use the last index instead

if numberTable[5] == numberTable[4] then
   print("Tie!")
else
    if numberTable[5] == ut then
        -- etc ...
    elseif numberTable[5] == ot then
        -- etc ...
    end
end
1 Like

The red error you see 403: Cannot write to DataStore ... will be gone if you check the box, logical issues will not be solved from API Access.

i think it actually might be an error with the code i wrote before now, the list is showing all of the values at the max health, even though i hurt a few of the buildings.

I found why the values werent updating, but the tie is still broken
image

it looks like the order is wrong tho so that might be it

1 Like