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.
01 | wait() |
02 | player = game.Players.LocalPlayer |
03 | hum = player.Character.Humanoid |
04 | local n = { "Player1" , "pmcdonough" } |
05 | hum.HealthChanged:connect( function () |
06 | if hum.Parent.Name = = "Player1" then --HERE lies the issues I am having. "Name == Player1 **or** pmcdonough" is the effect I would like. |
07 | if hum.Health < 100 then |
08 | repeat |
09 | hum.Health = hum.Health + . 01 |
10 | wait() |
11 | until hum.Health = = 100 |
12 | -- Doubling bit. Reason I need help compressing. |
13 | elseif hum.Parent.Name = = "pmcdonough" then |
14 | if hum.Health < 100 then |
15 | repeat |
As M39a9am3R pointed out, you can simply do:
1 | if hum.Parent.Name = = "this" or hum.Parent.Name = = "that" then |
2 | -- code |
3 | 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:
01 | local names = { "Player1" , "pmcdonough" } |
02 |
03 | -- Function that checks if a given name is in the names table |
04 | local 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! |
12 | 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!
1 | local 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.