Character is not a valid member of Player.PlayerObject, it's basic I just can't fix it for some reason?

I know this is a really simple problem but I’m pretty new to scripting but I’d like to fix this problem as I am getting this simple basic error that I know how to fix but I quite can’t:

“Character is not a valid member of ‘Player.ThoifeDev’”

I’ve fixed these types of errors with no problem, but the problem is I hate using normal scripts instead of local scripts.

The mission of this script is to let a certain user go in if their name is exclusively this, it’s post to be a bed-type thing for only devs in my game and if you touch it and you’re not a dev then it will teleport you.

Code:

script.Parent.Touched:Connect(function(hit)
	
	if hit.Parent.Character and hit.Parent.Name == "ThoifeDev" then
		script.Parent.CanCollide = false
	else
		hit.Parent.Character.HumanoidRootPart.CFrame = CFrame.new(119.61, 0.5, -180.22)
	end
	
	end)

The main line that’s causing me problems is line 3, probably line 5 too.

I just need to find the character as I’ve done before but I’ve never tried this type of thing.

hit.Parent is already the character model, so you can just do

if hit.Parent and hit.Parent.Name == "ThoifeDev" then
	script.Parent.CanCollide = false
else
	hit.Parent.HumanoidRootPart.CFrame = CFrame.new(119.61, 0.5, -180.22)
end
1 Like

But then it proceeds to find the accessory, not the character, and I don’t really want errors in my output, so I need a

if char then

statement but also using the hit.Parent.Name thingy.

image

I revised your script a little, try this out

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') ~= nil then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)

        if player.Name == 'ThoifeDev' then
            script.Parent.CanCollide = false
        else
		    player.Character.HumanoidRootPart.CFrame = CFrame.new(119.61, 0.5, -180.22)
	    end
    end
end)
2 Likes

Question, where exactly is this script located?

Also @lluckvy player might be defined as nil if you’re not implementing any conditional checks with it & the script detects a hit.Parent that is not a valid Player Object

script.Parent.Touched:Connect(function(hit)
    if hit.Parent and hit.Parent.Name == "ThoifeDev" then
        script.Parent.CanCollide = false
    else
        hit.Parent:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(119.61, 0.5, -180.22)
    end
end)
1 Like

It’s located parented to a part.

Why you use ~= nil if you check already if the humanoid exist without it?

It doesn’t really matter, it works both ways

if hit.Parent:FindFirstChild('Humanoid') ~= nil then
if hit.Parent:FindFirstChild('Humanoid') then

Yes I know, I just wanted to know why you use ~= nil if you don’t need it. :slightly_smiling_face: