Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why do I get this error?

Asked by
NRCme 0
8 years ago

In my LocalScript I get this weird error: 16:27:41.566 - Players.Player1.PlayerGui.Console2.Console.Input.LocalScript:39: attempt to perform arithmetic on field 'x' (a nil value)

But I don't know why...

My code:

local yesTime = 5
local yes = false
local chanceIP, chanceVirus, chanceFirewall, chanceTrace, chanceNetwork, chanceRemote, chancePhysical, chanceStealth, chanceLocal, chanceCracker, chanceLevel
local Skills = {
    ["IP"] = 1,
    ["virus"] = 1,
    ["firewall"] = 1,
    ["trace"] = 1,
    ["network"] = 1,
    ["remote"] = 1,
    ["physical"] = 1,
    ["stealth"] = 1,
    ["Local"] = 1,
    ["cracker"] = 1,
    ["level"] = 1
}   

local function chance(x)
    local rndmNeedWork = math.random(50)
    rndmNeedWork = rndmNeedWork * Skills.x - Skills.x
    if rndmNeedWork > 30 then
        yes = true
        wait(yesTime)
        yes =false
    end
end

while true do
        wait(0.1)
    chance("IP")
end

All help appreciated!

0
What is x supposed to be? An element in the dictionary? Sublimus 992 — 8y
0
Yeah NRCme 0 — 8y
0
Dictionary keys that are named have to be accessed by said names, this will be more difficult to implement then what you currently have. Also, dictionary/table keys are accessed by doing: thing[index] Sublimus 992 — 8y
0
Well I still need help.... NRCme 0 — 8y
View all comments (2 more)
0
maybe define x as something? iFireLazer 35 — 8y
0
that wouldn't work because then it would read what you assigned it to in the function not what the player types is the parameters NRCme 0 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

The script is trying to access the actual child of Skills called x, for example:

Skills.x = nil, so indexing it will return nil. To find a index in a table using it's key, you need to index it like this:

Skills[x]

Not:

Skills.x

Also, when defining the key of something, if you want it to be indexed by an unknown value (x, for example, which can be any string), you need to make sure the indexing is done like this:

array["keyString" or variableName]

And not like this:

array.keyString or array.variableName


So, instead of doing Skills.x, do:

Skills[x] = Skills[x] + Skills[x] -- or whatever you wanted to do with it

Hope I helped!

~TDP

0
why did you say array["keyString" or variableName] where would I put this NRCme 0 — 8y
0
It's an example, the key string is a string that is used to index the value, and the variableName is if you are indexing it with something like x. I will clarify. TheDeadlyPanther 2460 — 8y
0
that doesn't really help much.. that just changes the value of the dictionary item which isn't what I want NRCme 0 — 8y
Ad

Answer this question