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

Problem with my door?

function touch(hit)
	p = game.Players:GetPlayerFromCharacter(hit.Parent)
	if p.Backpack.Key ~= nil then
		script.Parent.CanCollide = false
		wait()
		script.Parent.Transparency = .5
	else
		print("Doesnt have key!")
	end
end

script.Parent.Touched:connect(touch)

When I touch the part with my key, it says:

Workspace.Part.Script:3: attempt to index global 'p' (a nil value)

2 answers

here I have an idea for you to identify the character

function touch(hit)
	if not hit.Parent:IsA("Model") then
		Character = hit.Parent.Parent -- because it might be a tool or a hat
	else
		Character = hit.Parent -- then its a character because the character is a model
	else
		return nil -- if its neither
	end
	local p = game.Players:GetPlayerFromCharacter(Character) -- gets player from the variable Character
	if p.Backpack.Key ~= nil then
 	       script.Parent.CanCollide = false
       		wait()
	        script.Parent.Transparency = .5
	else
	        print("Doesnt have key!")
	end
end
script.Parent.Touched:connect(touch)

please vote up if this helped

Since the key you're touching is the part, the part's parent is the tool. Maybe redirect the part to the tool to the character itself (hit.Parent.Parent). Or follow what coltn5000 demonstrated.

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