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

"attempt to index local 'hit' (a nil value)" Output Error?

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!

1 answer

✓ Accepted answer

The reason you're receiving this output is because the 'MouseButton1Click' event doesn't have any parameters which you can use.

The only way to get the player who clicked the button is by putting the SurfaceGui into each player's PlayerGui (via StarterGui), and treating it like an ordinary GUI. This means you'd get the player by parenting up to them, or in the case of a LocalScript you can use the 'LocalPlayer' property of the 'Players' service.

3 comments

0

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.

0

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).

0

That's an interesting approach, I'll definitely look into doing that. Thanks for the help!

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