ZelkenJoin Date: 2012-02-22 Post Count: 20 |
This is probably some beginner mistake but here's this script:
--- Variables ---
local plr = game.Players.LocalPlayer
local skill = plr.Tradeskills:FindFirstChild(script.SkillName.Value)
local skilllvl = script.SkillLevel.Value
local Resource = plr.Tradeskills:FindFirstChild(script.ResourceName.Value)
local RNeeded = script.Resource.Value
local item = game.ReplicatedStorage.ITEMS:FindFirstChild(script.Parent.Parent.Name)
--- Function ---
script.Parent.MouseButton1Click:connect(function()
if skill.Value >= skilllvl then
if Resource.Value >= RNeeded then
local itemclone = item:Clone()
itemclone.Parent = plr.Backpack
Resource = Resource - RNeeded
end
print(plr.Name.." doesn't have enough resources!")
end
print(plr.Name.."'s skill isn't high enough!")
end)
The error is in the title. |
|
CyrakohlJoin Date: 2014-11-09 Post Count: 3197 |
What line is the error on |
|
CyrakohlJoin Date: 2014-11-09 Post Count: 3197 |
It seems that you Indexed "skill" twice. |
|
VoxxieJoin Date: 2006-08-27 Post Count: 325 |
Hello Zelken!
I'm assuming your instances like "script.SkillLevel" and "script.Resource" are IntValues, or something similar.
Going off memory, you can not assign an instance's properties to a variable. ie: You can't do "local example = thing.Value"
I'd recommend changing your variables to, for example, 'local skilllvl = script.SkillLevel', and then in your function referencing the value as 'skilllvl.Value' |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
You can do local example = thing.Value, it just won't change with thing's value nor will it change thing's value when altered as it's just a reference to the value it holds.
|
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
* as it's just a variable with the same value, not a reference to the original value at all |
|
ZelkenJoin Date: 2012-02-22 Post Count: 20 |
@Cyrakohl
It's on line 12 |
|
ZelkenJoin Date: 2012-02-22 Post Count: 20 |
@Voxxie
I did that but it still doesn't work |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
Is it possible Resource returns nil? ( print(Resource) ) |
|
PreselectJoin Date: 2017-07-30 Post Count: 94 |
This is unrelated, but another thing you should do is check if the player has enough money via the server. Just a safety precaution! |
|
ZelkenJoin Date: 2012-02-22 Post Count: 20 |
Oh my bad it's on line 11. It has something to do with skilllvl |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
print skill, it's likely it returns nil |
|
ZelkenJoin Date: 2012-02-22 Post Count: 20 |
No, skill didn't return nil |
|
|
--- Variables ---
local plr = game.Players.LocalPlayer
local skill = plr.Tradeskills:FindFirstChild(script.SkillName)
local skilllvl = script.SkillLevel.Value
local Resource = plr.Tradeskills:FindFirstChild(script.ResourceName.Value)
local RNeeded = script.Resource.Value
local item = game.ReplicatedStorage.ITEMS:FindFirstChild(script.Parent.Parent.Name)
--- Function ---
script.Parent.MouseButton1Click:connect(function()
if skill.Value >= skilllvl then
if Resource.Value >= RNeeded then
local itemclone = item:Clone()
itemclone.Parent = plr.Backpack
Resource = Resource - RNeeded
end
print(plr.Name.." doesn't have enough resources!")
end
print(plr.Name.."'s skill isn't high enough!")
end)
served
served |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
In general 'attempt to index upvalue' indicates you're trying to index a member that doesn't exist for whatever reason. Your best bet is to print all the things you're trying to index (a.something, a["something"] etc) |
|
ZelkenJoin Date: 2012-02-22 Post Count: 20 |
Alright, I printed everything. Apparently Resource is nil. |
|
ZelkenJoin Date: 2012-02-22 Post Count: 20 |
Ok I changed it and now it works |
|
RerumuJoin Date: 2014-10-11 Post Count: 1489 |
For future reference, upvalues are local variables in a different stack depth.
local a = 5;
function Something()
local b = 4;
print(a, b); --< "a" is an upvalue, but b isn't
end; |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
@Shining, in this case the interpreter is assuming it's defined elsewhere (inaccessible) and thus providing that as likely reason? |
|
RerumuJoin Date: 2014-10-11 Post Count: 1489 |
Upvalues work the same as locals, but they just are defined out of scope because of how bytecode needs to be compiled.
Just referring to the fact one should be looking out of stack. |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
Ah ok, along with the lua docs that does make sense, never really stopped to consider the way scoping works in compilation. Thank you for elaborating |
|