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

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

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

✓ Accepted answer

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)

2 comments

0

Ahh thank you so much man, good to know for the future

0

no problem!

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)

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