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

Asked by 7 years ago
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)

0
Mistake, meant to say StarterGUI Ex_plore 62 — 7y

2 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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.

Ad
Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago

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.

Answer this question