Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Error appears even if no one touched the brick?

Asked by 6 years ago
Edited 6 years ago

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

0
GetPlayerFromCharacter will retunr nil if the player is not found allow for this case in your code User#5423 17 — 6y

2 answers

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago

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)
Ad
Log in to vote
0
Answered by 6 years ago

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)

Answer this question