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 number value?

script.Parent.MouseButton1Down:Connect(function(hit)
	a=script.Parent.Parent.TextBox.Text
	b=game.Lighting.Keycard
	c=b:Clone() d=Instance.new("IntValue")
	c.Parent=hit.Backpack
	d.Parent=c
	d.Value=a
end)

For some reason this script won't work. Basically when I click a button, its supposed to get the text from the textbox, and then create a keycard with that value, however it doesn't work. It says. The error from below points to the script I have shown in the example however, it does not not help me know what the problem is. The gui IS in the PlayerGUI by the way, not starter gear.

13:43:23.565 - Players.Player1.PlayerGui.ScreenGui.Main.TextButton.Script:5: attempt to index local 'hit' (a number value)

1 comment

0

Mistake, meant to say StarterGUI

2 answers

✓ Accepted answer

Your Problem

The MouseButton1Down event returns two values: The x and y coordinates of where you clicked on the UI element.

You are using the returned 'x' coordinate like it's the Player object.


Fix?

Reference the player by indexing the 'LocalPlayer' from game.Players - This will always return the player object when used from a LocalScript.

Note: Using meaningful variable names will help your code become neater and more legible.

local plr = game.Players.LocalPlayer; --This is the player object
local keycard = game.Lighting.Keycard --This is the keycard

--^Defined on the outside of the event because there's no need to define it everytime the event fires.

script.Parent.MouseButton1Down:Connect(function()
	local text = script.Parent.Parent.TextBox.Text
	local c = keycard:Clone() 
	local v = Instance.new("IntValue")
	v.Value = text
	v.Parent = c
	c.Parent = plr.Backpack
end)

Another note: Lighting is not meant for storage. Consider using ReplicatedStorage for this instead.

This is a gui button. The arguments given automaticly for the click function on gui's are MouseX, MouseY.

Since this is a gui, you can get the current player using game.Players.LocalPlayer.

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