Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix this script that open a door when a player on a certain team touches it?

I have a script in a part and when a player on a certain team touches it, the part CanCollide goes false.

local door = script.Parent

door.Touched:Connect(function(Door)
	local character = Door.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(character)
	if character then
		if player.Team == game:GetService('Teams').TEAMNAME then
			door.CanCollide = false
		else
		--SCRIPT HERE
		end
	end
end)

However when a player on that certain team tries to touch the door, it ends with an error in the output attempting to index nil with 'Team' at line 7. How do I make it so this script does what I want?

3 answers

✓ Accepted answer

Here is the fix

local door = script.Parent

door.Touched:Connect(function(Door)
    local character = Door.Parent:FindFirstChild("Humanoid") and Door.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    if character then
        if player.Team == game:GetService('Teams').TEAMNAME then
            door.CanCollide = false
        else
        --SCRIPT HERE
        end
    end
end)

You were setting the Character as a Humanoid. They are two completely different things. The humanoid is hereditary of the Character. Therefore, you can just do what I did.

local character = Door.Parent:FindFirstChild("Humanoid") and Door.Parent

This snippet of code is just a shortcut so you don't have to go through any "if" statements. Simple boolean logic. When using this, it should also be the second argument.

If we were to consider A and B, it would choose B. If you said B and A it would choose A.

The first one is the logic, the second one is the variable.

Hopefully that makes sense...

Players:GetPlayerFromCharacter() need the model of the character

local door = script.Parent

door.Touched:Connect(function(Door)
    local character = Door.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    if character then
        if player.Team == game:GetService('Teams').TEAMNAME then
            door.CanCollide = false
        else
        --SCRIPT HERE
        end
    end
end)
local door = script.Parent

door.Touched:Connect(function(Door)
    local character = Door.Parent:FindFirstChild("Humanoid")
    local player = game.Players:GetPlayerFromCharacter(Door.Parent)
    if character then
        if player.Team == game:GetService('Teams').TEAMNAME then
            door.CanCollide = false
        else
        --SCRIPT HERE
        end
    end
end)

The error with this is that character is the humanoid. You then try to get the character's player from their humanoid, which does not work. By changing it to Door.Parent, it takes the actual character and finds its character.

Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.