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

Is there somthing in this if statement that I did incorectly?

Asked by 6 years ago

I'm trying to create a script that let's the player in first person mode see there arms, torso, and legs. I ran into an issue where I kept on getting this error, luckily it didn't cause the script to shut down, but it's clogging up Output and I'm afraid it might slow performance. Here is the error

18:41:31.492 - LocalTransparencyModifier is not a valid member of Humanoid 18:41:31.492 - Stack Begin 18:41:31.493 - Script 'Workspace.gaberdell.HumanoidRootPart', Line 16 18:41:31.493 - Stack End

The code below

-- This Piece of Code is Designed so the Players Can See There Arms, Torso, and Legs, To Make the Game Feel More Like an FPS

while script.Parent.ClassName ~= "Model" do wait() end -- This is just to check if we are in the players model

local playerCharacter = script.Parent -- This vairiable is put in here to make the code easier to understand

game:GetService("RunService").RenderStepped:connect(function() -- We get renderstepped roblox's faster version of the While loop and no wait values are required as this alone won't crash the game

    for num, object in pairs(playerCharacter:GetChildren()) do -- This line "finds" all of the Objects Located Inside of The Player's Model
        if playerCharacter:FindFirstChild(object.Name) then -- Here we check to see if the Part exsists (Just in case)

            if object.Name ~= "Head" or "HumanoidRootPart" and object.ClassName == "Part" then -- We find out if it's one of the parts we don't want to be transperent and is a brick

                playerCharacter[object.Name].LocalTransparencyModifier = 0 -- Why we set LocalTransparencyModifier to 0 as opposed to regular Transparency is because where undoing roblox scripts that make it 1

            end
        end
    end
end)
0
As the output says. You're trying to set the transparency of the Humanoid, which is not a part. Add "object.Name ~= "Humanoid"" Mayk728 855 — 6y
0
A humanoid is not a Part, they check for that. User#18718 0 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

So this comes down to "order of operations" of lua if statements. The offending line is number 12 which is.

if object.Name ~= "Head" or "HumanoidRootPart" and object.ClassName == "Part" then

There are two things that are wrong / unintended with this line.

The statement interprets as the following to the roblox game engine.

is the name of the object not equal to head, then continue. is my string not nil (yes) and is the object classname "Part". (Part should actually be "BasePart")

The problem with this is that it will not continue after reading the first part of the if statement (most of the objects in the character will not be named Head and immediately pass this). The second problem is that to check multiple strings you have to write them all out, the syntax str = "foo" or "bar" is NOT correct.

Here's how I would rewrite your if statement (again line number 12).

if object.Name ~= "Head" and object.Name ~= "HumanoidRootPart" and object:IsA("BasePart") then

I used ands instead because a name can "not be" multiple strings at the same time. Also I use the :IsA method for a character.

There are a few more improvements that could be made. You don't need to check if an object exists if you are already given it, and you don't need to look up objects twice with character[object.Name] just using the variable object is fine. :)

Here's how I would suggest rewriting the entire code, but it's up to you. (tested)

-- I removed your comments to make it more readable for myself. You are welcome to borrow
-- as much code as you want, and leave out some others.

assert(script.Parent:IsA("Model")) -- we can throw one error if we aren't in a character. It won't run before then.

local playerCharacter = script.Parent

game:GetService("RunService").RenderStepped:connect(function()
    for num, object in pairs(playerCharacter:GetChildren()) do
        if object.Name ~= "Head" and object.Name ~= "HumanoidRootPart" and object:IsA("BasePart") then
            object.LocalTransparencyModifier = 0
        end
    end
end)

edit: extra note: cool concept to keep the arms and torso on screen for a FPS feel it was cool trying it out in studio!

0
I didn't read the title very well. Now I can see you knew it was the if statement all along. User#18718 0 — 6y
0
Oh it's okay. Thank you so much for explaining how "or" statements worked this helped me understand lau alot more! gaberdell 71 — 6y
Ad

Answer this question