As the title states, I'm receiving the output error:
Workspace.Part.SurfaceGui.Frame.Frame.Frame.TextButton.Scri:7: attempt to index local 'hit' (a nil value)
Whenever I use this script:
local price = 50
local light = game.Lighting.globaltyc
local ipath = game.Workspace.Tycoon.Tycoon.InvInt
script.Parent.MouseButton1Click:connect(function(hit)
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
local stats = user:findFirstChild("leaderstats")
player = game.Players[hit.Name]
if game.Players:findFirstChild(hit.Name) == light.TycUser1.Value then
ipath.Stone.Value = ipath.Stone.Value - 1
if stats ~= nil then
local cash = stats:findFirstChild("Cash")
cash.Value = cash.Value + price
end
end
end)
A few important things about the script:
- It is NOT a localscript
- It is inside of a textbutton GUI element
- I have used similar scripts to this, and haven't had to define "hit".
- I've read on the Wiki that it is caused by an undefined parameter.
As always, any help is greatly appreciated, and thank you for reading!

3 comments
Oh, this is a bit discouraging. Will any of the other Mouse events available to a text button have the ability to gather the player? If not, it looks like the best way is to just make it a block that looks like a surface GUI.
As far as I'm aware, ROBLOX has no plans to support this. Also, instead of putting the whole SurfaceGui into the player's PlayerGui, you could always separate the buttons from the rest of the GUI and put them into SurfaceGuis (which are given to every player).
That's an interesting approach, I'll definitely look into doing that. Thanks for the help!