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

attempt to index local 'Hit', a nil value?

Asked by
Kitorari 266 Moderation Voter
7 years ago
Edited 7 years ago
script.Parent.MouseButton1Click:connect(function(Hit)
    pcall(function()
    local p = game.Players:GetPlayerFromCharacter(Hit.Parent):WaitForChild("Folder"):WaitForChild("Buns")
    if p.Value>= 2
        then print("Yay")
    end
    end)
end)

This script is attempting to check if a players "Buns" Value equals more then 2 or is equal to 2. There is no errors, but it does not function.

script.Parent.MouseButton1Click:connect(function(Hit)
    local p = game.Players:GetPlayerFromCharacter(Hit.Parent):WaitForChild("Folder"):WaitForChild("Buns")
    if p.Value>= 2
        then print("Yay")
    end
end)

This script does error. the only thing removed though is PCALL.

21:25:02.248 - Workspace.Part.SurfaceGui.Frame.Blue Pen.Script:2: attempt to index local 'Hit' (a nil value) 21:25:02.248 - Stack Begin 21:25:02.249 - Script 'Workspace.Part.SurfaceGui.Frame.Blue Pen.Script', Line 2 21:25:02.249 - Stack End

((And yes, The Buns is set to 2.))

1 answer

Log in to vote
1
Answered by
dreamy67 135
7 years ago

Surface Guis can not detect who has clicked them if they are in the workspace. To find out who clicked them, you must put the Surface Gui inside the StarterGui and Adornee it to the part in workspace. To do this, put a local script inside the Surface Gui and type,

script.Parent.Adornee = game.Workspace.Part --(or whatever your part name is)

Then inside your button, you can define a variable with game.Players.LocalPlayer and use it inside the MoudeButton1Click function as the player who clicked the button.

0
Oh. Lovely. Welp im just gonna stick to screen gui's theenn Kitorari 266 — 7y
Ad

Answer this question