Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why do I get this error?

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!

6 comments

0

What is x supposed to be? An element in the dictionary?

0

Yeah

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]

0

Well I still need help....

0

maybe define x as something?

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

1 answer

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

3 comments

0

why did you say array["keyString" or variableName] where would I put this

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.

0

that doesn't really help much.. that just changes the value of the dictionary item which isn't what I want

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