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 'hit' a nil value. Why is this error happening?

So I'm making a shop. VERY basic one. Nothing to complicated, just so I can catch on. Here's what I have so far:

local Button = script.Parent
local gui = game.Workspace.surface.SurfaceGui.TextButton
local db = true

function check(hit)
	local character = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		   if character.leaderstats.Money.Value >= 5 then
				
				character.leaderstats.Money.Value = character.leaderstats.Money.Value - 5
				
				game.Lighting.Lantern:Clone().Parent = character.Backpack
				
				
				
		else
			db = false
				gui.Text = "You need more money!"
				wait(2)
				gui.Text = " "
				
				db = true
	
			end
		end


Button.MouseButton1Click:connect(check)

There's an error for some reason. I have no idea what I'm doing wrong. Everything works fine if I change it to a Touched event, but when I try to make a click event, nothing works. Any ideas? All help is appreciated!

1 answer

✓ Accepted answer

MouseButton1Click does not send any parameters to the connected function.

Put your SurfaceGui in game.ReplicatedStorage

then put in StarterCharacterScripts in a LocalScript:

local plr = game.Players.LocalPlayer
local clone = game.ReplicatedStorage:WaitForChild("GUISNAME"):Clone()
clone.Parent = plr.PlayerGui
clone.Adornee = workspace:WaitForChild("surface") --> must be the part you want the SurfaceGui to appear on

then in the script in your actual TextButton (make it a localscript):

local plr = game.Players.LocalPlayer
local Button = script.Parent
--local gui = nil -- isn't button same as gui?
local db = true

function check()
	if plr.leaderstats.Money.Value >= 5 then

		plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - 5

		game.Lighting.Lantern:Clone().Parent = plr.Backpack



	else
		db = false
		button.Text = "You need more money!"
		wait(2)
		button.Text = " "

		db = true

	end
end


Button.MouseButton1Click:connect(check)

9 comments

0

I'm confused. So, what's the solution? Where do I insert the "local plr = script.Parent.PARENTTOPLAYER

0

Well if this is in a SurfaceGui in workspace3 there is now way to get to know who clicked it, you will have to put the SurfaceGui in the player, handle the GUI locally and set the SurfaceGui's Adornee to the part in workspace instead. (yes it's confusing)

0

How can I put SurfaceGui into player? I'm sorry if I don't catch on, but I'm completely confused. The gui is in a brick. inside the gui there is a TextButton followed by a script. What does PARENTTOPLAYER mean?

0

(I have updated my answer)

0

Note that this answer is not FilteringEnabled compatible. Also, you don't use 'db' in your second script, you just assign to it.

0

Nope it's not FilteringEnabled compatible, I only changed it so that it should work (and didn't expect op to use FE)

0

Ok, I think I need to start a bit lower than this. It's too hard for me to understand. I'll come back to this later on, once I understand fundamental things. I'll accept your answer though, since I'll go back to this in the future. Thanks so much!

0

well did it work? >;3

0

I'm not sure. I haven't tried it. I just don't like inserting something I don't understand. That'd defeat the purpose of me learning. But, I'll get back to this for sure to see if it works.

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