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

Why does this error? "SampleText" is not a valid member of

Asked by 2 years ago
Edited 2 years ago

Ok a quick explanation, This tool executes the "else" part if it doesn't find "PLRcrushed"

And that works, the problem is at the beginning I set "MPART" to "SampleText" but once the function is fired if the humanoid is R6 or R15 it sets "MPART" to a different string, though, it doesn't work.

The code errors and says "SampleText" is not a valid member of character, why? I'm literally setting it to a different string, Am I dumb or what am I missing

The variable is used later on the script as "plr[MPART]"

I'm pretty new to scripting tbh so sorry if the code is messy

local toolio = script.Parent
local Sounds = toolio.Handle.Sounds
local MPART = "SampleText"

function work(_,hit)
    if hit:IsA("Part") or hit:IsA("MeshPart") then
        if hit.Parent.ClassName ~= "Accessory" then
            if hit.Parent:FindFirstChild("Humanoid") then
                plr = hit.Parent
            end
        elseif hit.Parent.ClassName == "Accessory" then
            plr = hit.Parent.Parent
        end

        if plr:FindFirstChild("PLRcrushed") then
            print("Player has been crushed already")
        else
            if plr.Humanoid.RigType == Enum.RigType.R6 then
                MPART = "Torso"
            elseif plr.Humanoid.RigType == Enum.RigType.R15 then
                MPART = "LowerTorso"
            end

(Keep in mind I know a lot of scopes aren't closed, that's because it isn't the full script, i sent the part that errors)

1 answer

Log in to vote
1
Answered by
TGazza 1336 Moderation Voter
2 years ago

try this, Ive made some changes to what you have to make sure it doesn't error out...

local toolio = script.Parent
local Sounds = toolio.Handle.Sounds
local MPART = "SampleText"

--// new function to grab the player and the character from a part
function getCharacterAndPlayerfrom(part)
    local ws = part.Parent
    local Char
    local Player
    while ws ~= workspace do
        Char = ws
        ws = ws.parent
        if(ws == workspace) then
            break
        end
    end
    if(Char ~= nil) then
        Player = game.Players:getPlayerFromCharacter(Char)
    end
    return Player, Char
end

function work(_,hit)
    if hit:IsA("Part") or hit:IsA("MeshPart") then
       --[[ if hit.Parent.ClassName ~= "Accessory" then
            if hit.Parent:FindFirstChild("Humanoid") then
                plr = hit.Parent
            end
        elseif hit.Parent.ClassName == "Accessory" then
            plr = hit.Parent.Parent
        end]]
        local Player,Char = getCharacterAndPlayerfrom(hit)

        if(Char ~= nil) then
        --// is this part meant to be inside the player not the character? if it is then change Char to Player!
            if Char:FindFirstChild("PLRcrushed") then
                print("Player has been crushed already")
            else
                if Char.Humanoid.RigType == Enum.RigType.R6 then
                    MPART = "Torso"
                elseif Char.Humanoid.RigType == Enum.RigType.R15 then
                    MPART = "LowerTorso"
                else
                --// Something else touched us 
                    warn(hit,"[",hit.Parent,"] Touched us!!, \n Setting MPART to ERROR!", debug.traceback())
                    MPART = "ERROR!"
                end
            end
        end
    end
end

The new function will return nil on both player and character variables if the thing that has touched this object isn't a player. Like for instance a bullet fired from a gun is not a player etc...

Hope this helps!

0
"ERROR!" is not a valid member of Model, Maybe i should have been more specific, but hit is the target of the mouse, And i need to know why doesn't MY script work, the variable plr refers to the character, which works perfectly, but when trying to look for the RigType in humanoid it just skips it, why? SharkRayMaster 265 — 2y
0
I'm so confused, I really don't understand, the script works now but all I did was change "Enum.RigType" to "Enum.HumanoidRigType", I can't believe that was the problem, I appreciate your help tho! SharkRayMaster 265 — 2y
Ad

Answer this question