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

Attempting to use Elseif?

Asked by
Smoho 2
6 years ago
Edited 6 years ago

In my game, I have multiple characters which have different parts for their arms/hands. For example, one is a treasure chest that has a lock with hands. Another doesn't have hands to hold an item with, so it uses its teeth. So, a potential solution for holding items was to adapt items by giving them a welding script.

However, the elseif/else if statements are being ignored, and the script only accounts for the very first if statement. The only output error is that if the character does not have the first statement's part, it will not be a valid member of the model. The script works fine for the first statement's character, however.

Here is a snippet of the script, for some context:

script.Parent.AncestryChanged:connect(function()
local Char= script.Parent.Parent
local weld = Instance.new("Weld")

if Char.ArmR ~= nil then
    weld.Parent = Char.ArmR
        weld.Part0 = Char.ArmR
        weld.Part1 = script.Parent.Handle
elseif Char.Bar ~= nil then
weld.Parent = Char.Bar
        weld.Part0 = Char.Bar
        weld.Part1 = script.Parent.Handle
 elseif Char.LegFR ~= nil then
    weld.Parent = Char.LegFR
        weld.Part0 = Char.LegFR
        weld.Part1 = script.Parent.Handle

The script is basically supposed to be saying "If the parent of this tool changes, check the character for "ArmR", and if it doesn't, check if it has "Bar", and so on... Once the arm is found, the weld is formed.". But instead, it seems to say "If the parent of this tool changes, check the character for ArmR."

I have looked at a few tutorials about using Else/Elseif statements, and they seem to be structured this way, so I am confused. Extra context: It is stored in a regular script, as a child of the Tool. The tool is stored within StarterPack, so it is immediately moved to the Backpack.

0
When you get an error the script will stop running. You need to make sure it doesn't error when the child doesn't exist, you need to use FindFirstChild [if Char:FindFirstChild("ArmR") then] gullet 471 — 6y
0
^ Use FindFirstChild on the Character since you want to check if a descendant there exists Shawnyg 4330 — 6y

1 answer

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
6 years ago

The . is used for properties, or a descendant of an instance. By using it, you're telling the script you already know the descendant exists. Instead, use FindFirstChild which will return true if the object is found, and false if it isn't. You'd use it on lines 5,9,and 13 instead of using the . (can't remember the technical name for the '.' in programming)

0
Sort of like "if plr:FindFirstChild("ArmR") == true then"? Smoho 2 — 6y
0
Because now it won't weld to any character, including the first one listed Smoho 2 — 6y
0
Not on the player, the Character. You can also remove the == true part, since it is a conditional statement and FindFirstChild return true/false Shawnyg 4330 — 6y
0
Sorry about saying plr, I meant character! Anyway, thank you! I changed it from == true to ~= nil, and it worked perfectly! Smoho 2 — 6y
0
technical name is probably indexing since you can index tables using x.y User#22604 1 — 6y
Ad

Answer this question