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 8 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.

01wait()
02player = game.Players.LocalPlayer
03hum = player.Character.Humanoid
04local n = {"Player1", "pmcdonough"}
05hum.HealthChanged:connect(function()
06if hum.Parent.Name == "Player1" then--HERE lies the issues I am having. "Name == Player1 **or** pmcdonough" is the effect I would like.
07if hum.Health < 100 then
08repeat
09hum.Health = hum.Health + .01
10wait()
11until hum.Health == 100
12-- Doubling bit. Reason I need help compressing.
13elseif hum.Parent.Name == "pmcdonough" then
14if hum.Health < 100 then
15repeat
View all 22 lines...
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 — 8y
0
Thank you, that was the missing info. Due to previous attempts I thought or wouldn't work in that way. pmcdonough 85 — 8y

1 answer

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

As M39a9am3R pointed out, you can simply do:

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

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:

01local names = { "Player1", "pmcdonough" }
02 
03-- Function that checks if a given name is in the names table
04local function IsValidName(name)
05    -- iterator over the table and compare each value with name
06    for idx = 1, #names do
07    if names[idx] == name then
08      return true -- name is in the table!
09    end
10  end
11  return false -- name is not in the table!
12end

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!

1local names = {
2  ["Player1"] = true,
3  ["pmcdonough"] = true
4}

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