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)
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!