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
6 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:

1script.Parent.Touched:connect(function(ply)
2if game.Players:findFirstChild(ply.Parent.Name) then
3if game.Players[ply.Parent.Name]:IsInGroup(3377124)==true then
4script.Parent.CanCollide = false
5wait(2)
6script.Parent.CanCollide = true
7end
8end
9end)

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
6 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:

1script.Parent.Touched:Connect(function(ply)
2    if ply and ply.Parent then
3         ...
4    end
5end)
0
Ahh thank you so much man, good to know for the future Azuc 112 — 6y
0
no problem! thebayou 441 — 6y
Ad
Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
6 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.

01local debounce = true
02 
03script.Parent.Touched:connect(function(hit)
04    --First make sure what has hit the door is really a player
05    if hit.Parent:FindFirstChild("Humanoid") then
06        --Check if they're in group. You can leave the "==true" part out since this will return a bool of either true or false.
07        if game.Players[hit.Parent.Name]:IsInGroup(3377124) then
08 
09            --A debounce is used for safety
10            if debounce then
11                debounce = false
12                script.Parent.CanCollide = false
13                wait(2)
14                script.Parent.CanCollide = true
15                debounce = true
16            end
17        end
18    end
19end)

Answer this question