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

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

Asked by 7 years ago

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)
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. M39a9am3R 3210 — 7y
0
Thank you, that was the missing info. Due to previous attempts I thought or wouldn't work in that way. pmcdonough 85 — 7y

1 answer

Log in to vote
3
Answered by
jakedies 315 Trusted Moderation Voter Administrator Community Moderator
7 years ago

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.

Ad

Answer this question