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

Is there a method of an "or" in the vocabularic sense during a condition?

What I mean is, in the problematic area indicated below, where I should be able to make the script half as long I couldn't. I tried inpairs, strings, variables, and some other unmemorable attempts.. I just know too little about what I am doing to fix the problem.

wait()
player = game.Players.LocalPlayer
hum = player.Character.Humanoid
local n = {"Player1", "pmcdonough"}
hum.HealthChanged:connect(function()
if hum.Parent.Name == "Player1" then--HERE lies the issues I am having. "Name == Player1 **or** pmcdonough" is the effect I would like.
if hum.Health < 100 then
repeat
hum.Health = hum.Health + .01
wait()
until hum.Health == 100
-- Doubling bit. Reason I need help compressing.
elseif hum.Parent.Name == "pmcdonough" then
if hum.Health < 100 then
repeat
hum.Health = hum.Health + .01
wait()
until hum.Health == 100
end
end
end
end)

2 comments

1

You would have to complete the condition. You need to compare the humanoid parent's name to both Player1 and pmcdonough. So your if then would have to be: if hum.Parent.Name == "Player1" or hum.Parent.Name == "pmcdonough" then. Otherwise you're just identifying if that string value exists, which of course it does you made it.

0

Thank you, that was the missing info. Due to previous attempts I thought or wouldn't work in that way.

1 answer

✓ Accepted answer

As M39a9am3R pointed out, you can simply do:

if hum.Parent.Name == "this" or hum.Parent.Name == "that" then
  -- code
end

However, this is not a very clean solution. It would be ideal to have a table with all the names, and to check if they should, in this case, heal then it should be as simple as checking if it is in the table.

A common approach would be to do something of this sort:

local names = { "Player1", "pmcdonough" }

-- Function that checks if a given name is in the names table
local function IsValidName(name)
	-- iterator over the table and compare each value with name
	for idx = 1, #names do
    if names[idx] == name then
      return true -- name is in the table!
    end
  end
  return false -- name is not in the table!
end

And then you can check if their name is in the table when needed by doing: if IsValidName(hum.Parent.Name) then

However there is a more simple approach: use a dictionary!

local names = {
  ["Player1"] = true,
  ["pmcdonough"] = true
}

And now all you have to do to check if their name is in the table is this: if names[hum.Parent.Name] then No function calls, no explicit loops. It's clean, simple, easy to expand. It's beautiful. It's Lua.

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