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

Error appears even if no one touched the brick?


Hi guys

So i have a brick,that makes a gui appear.There are no errors in the script,and when the server starts this happens in the output:

16:53:25.182 - Workspace.Buildings.islands.Lobby.Stall.Touchy.Script:3: attempt to index local 'plr' (a nil value)

It keeps appearing until my error logger cannot take it anymore and says:

16:53:25.294 - HTTP 429 (HTTP/1.1 429 TOO MANY REQUESTS)

What is going on?heres the script

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	plr.PlayerGui.MainMenu.Visible = true
	plr.PlayerGui.Note.Visible = true
end)

it says that it is attempting to index a local plr(a nil value) even if there were no errors in the script.what does this mean?Please help

1 comment

0

GetPlayerFromCharacter will retunr nil if the player is not found allow for this case in your code

2 answers

✓ Accepted answer

That's because plr doesn't exist. If the brick is not part of a character, then GetPlayerFromCharacter will return nil, so plr will be nil, which is causing your problem. The code on line 3 should fix your problem. It ends the function if player is nil.

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr == nil then return end
	plr.PlayerGui.MainMenu.Visible = true
	plr.PlayerGui.Note.Visible = true
end)

Upon the game starting, the first thing your Part touches is the baseplate. What you're lacking of is a humanoid check (people usually use this to check for players touching a part). The error pops because baseplate does not consist of PlayerGui yet you try to set the value of a non existent setting. Here's what you should do

script.Parent.Touched:Connect(function(hit)
	--I Suggest you adding a debounce here else it will over print
	humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
	    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	    plr.PlayerGui.MainMenu.Visible = true
	    plr.PlayerGui.Note.Visible = true
	end
end)

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