generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

attempt to index local 'hit' (a nil value)

Previous Thread :: Next Thread 
ROBOFROG is not online. ROBOFROG
Joined: 04 Jan 2009
Total Posts: 1519
30 Aug 2014 09:57 AM
As the title states, I'm receiving the output error:

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!
Report Abuse
Waffloid is not online. Waffloid
Joined: 14 Jul 2011
Total Posts: 1606
30 Aug 2014 10:01 AM
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)
if hit then
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
end)


yip yip yippady yip yip
Report Abuse
ROBOFROG is not online. ROBOFROG
Joined: 04 Jan 2009
Total Posts: 1519
30 Aug 2014 10:03 AM
I tried your script exactly, but sadly, it's still giving the same output error (Here's the full error, seeing as I forgot the first part in the main post):

Workspace.Part.SurfaceGui.Frame.Frame.Frame.TextButton.Scri:7: attempt to index local 'hit' (a nil value)
Report Abuse
Waffloid is not online. Waffloid
Joined: 14 Jul 2011
Total Posts: 1606
30 Aug 2014 11:18 AM
local price = 50
local light = game.Lighting.globaltyc
local ipath = game.Workspace.Tycoon.Tycoon.InvInt

script.Parent.MouseButton1Click:connect(function(hit)

if hit then
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
end)

yip yip yippady yip yip
Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
30 Aug 2014 11:52 AM
The problem with ur scipt is right here:


script.Parent.MouseButton1Click:connect(function(hit) -- don't use Button. Use a Click detector or touch interest or anything else, because Player can "Click" on ANYTHING, and u must hadle every case...

if hit then -- So even if this solves ur first error.......
local user = game.Players:GetPlayerFromCharacter(hit.Parent) -- This will error, if Player Clicks on the the Sky, because the Sky does not have a Parent...


For reference I'll give a function, which does try to process, a mouse click, but like I say, do it some other way:



function ProcessBit(b)
if b and b.Parent ~= Workspace then

if b.Parent == container then --- not sky or sea floor
Hex = Globe[b.Name]
bit = b
return true
else
b = b.Parent

if b.Parent ~= Workspace then
if b.Parent == container then
Hex = Globe[b.Name]
bit = b
return true
else

b = b.Parent
if b.Parent ~= Workspace then
if b.Parent == container then
Hex = Globe[b.Name]
bit = b
return true
else
return false
end

else
return false
end
end
else
return false
end
end -- Bin?
else
return false
end -- sky?

end -- fuction



Mouse.Button1Down:connect(function()
-- print (Mouse.Hit)
-- print (Mouse.Target)
I = ClickSound:Clone()
I.Parent = container
I.PlayOnRemove = true
I:Destroy()

bit = Mouse.Target
-- Spot = (Mouse.Hit)
print("Button 1 is down on ", bit)

if ProcessBit(bit) then
.
.
.


Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
30 Aug 2014 12:01 PM
*Sorry: Wrong Method, but first thing u should try is this:

script.Parent.MouseButton1Click:connect(function(hit)
print(hit)
Report Abuse
ROBOFROG is not online. ROBOFROG
Joined: 04 Jan 2009
Total Posts: 1519
30 Aug 2014 02:08 PM
The new method didn't work either, Waffloid. After putting in a few prints, it never got past the first "if" and didn't output anything. Thanks for trying, however!

And thank you for the attempt to help, bjcarpenter. But, sadly, I found there is no way to detect who clicked the GUI when using TextButton methods, unless it's inside a local script.

So, basically, there's no way this script can with without heavy design modification.
Report Abuse
Waffloid is not online. Waffloid
Joined: 14 Jul 2011
Total Posts: 1606
30 Aug 2014 02:13 PM
if it is a nil parameter, means that 'hit' == nil always

yip yip yippady yip yip
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image