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

I keep getting an error when I don't see any red "lines"?

I get this error in output: 17:41:04.203 - Workspace. .Talk.Script:3: attempt to index local 'plr' (a nil value) but I don't see any of those "red lines" in the script, and I have no idea why its saying plr isn't a variable.

Script:

script.Parent.Touched:connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if hit.Parent.ClassName == "Hat" and plr.PlayerGui.Talk then
		print("...")
	else
		h = hit.Parent.Humanoid
		if h ~= nil then
			game.ReplicatedStorage.Talk:Clone().Parent = plr.PlayerGui
		end
	end
end)

1 answer

✓ Accepted answer

Red lines only appear when there is a syntax error. Blue lines appear when you use a deprecated or nonexistent variable.

Your problem is that the GetPlayerFromCharacter method is returning nil. If something that is not a character touches the part that method will not be able to find the player and will return nil.

You will have to modify the below code because if plr is found then hit.Parent cannot be a Hat therefore it will never be called.

I also added FIndFirstChild to prevent future errors.

script.Parent.Touched:connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		if hit.Parent.ClassName == "Hat" and plr.PlayerGui:FindFirstChild("Talk") then
			print("...")
		  else
			 h = hit.Parent:FindFirstChild("Humanoid")
			if h ~= nil then
			game.ReplicatedStorage.Talk:Clone().Parent = plr.PlayerGui
		end
	end
end)

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