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

Having an error with a group only door, not sure what the error means exactly?

Asked by
Azuc 112
5 years ago

Basically the door works, but its throwing an error and I have a ton of them in game so its a big deal in the console.

Code:

script.Parent.Touched:connect(function(ply)
if game.Players:findFirstChild(ply.Parent.Name) then
if game.Players[ply.Parent.Name]:IsInGroup(3377124)==true then
script.Parent.CanCollide = false
wait(2)
script.Parent.CanCollide = true
end
end
end)

Error:

Workspace.Door.Script:2: attempt to index global 'player' (a nil value) Stack Begin Script 'Workspace.Door.Script', Line 2 Stack End

2 answers

Log in to vote
0
Answered by
thebayou 441 Moderation Voter
5 years ago

It means that the variable player has a value that is nil, as the error says. This could be caused if the parent of ply is nil, or if ply itself is nil. You can fix this by checking if ply exists and if it has a parent:

script.Parent.Touched:Connect(function(ply)
    if ply and ply.Parent then
         ...
    end
end)
0
Ahh thank you so much man, good to know for the future Azuc 112 — 5y
0
no problem! thebayou 441 — 5y
Ad
Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago

The touched event fires when anything touches the part. It can be the baseplate or any other parts that are not character limbs.

To fix this you'll need to check if ply, or hit since that's what I used to refer to a part touching, can find the Humanoid of it's parent. Remember, all characters have a humanoid.

I also use a debounce to make sure our Touch event isn't firing every single time when a part is touching it.

local debounce = true

script.Parent.Touched:connect(function(hit) 
    --First make sure what has hit the door is really a player
    if hit.Parent:FindFirstChild("Humanoid") then
        --Check if they're in group. You can leave the "==true" part out since this will return a bool of either true or false.
        if game.Players[hit.Parent.Name]:IsInGroup(3377124) then

            --A debounce is used for safety
            if debounce then
                debounce = false
                script.Parent.CanCollide = false
                wait(2)
                script.Parent.CanCollide = true
                debounce = true
            end
        end
    end
end)

Answer this question