Strange Error with Tables

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Twitter code UI, I have everything done, it was working, then suddenly this happened:

  2. What is the issue? attempt to index field '?' (a nil value)
    on this line:

local players = game:GetService("Players")
local dataStore2 = require(1936396537)
local CodeStoreName = "TwitterCodes"
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local function setupUserData()
	local Codes = {
		["YEET1"] = {Used = false, Reward = 500}
		}
	return Codes
end

ReplicatedStorage.TwitterCode.OnServerEvent:Connect(function(player, input)
	local Codes = dataStore2(CodeStoreName, player):Get(setupUserData())
	if Codes[input].Used == false then -- this line
  1. What solutions have you tried so far? I tried putting tostring(), nothing changed.

Every help from you is appreciated, I’m sure I pass the input so, I have no idea where the problem can be.

If are you wondering, this is the client side:

local RS = game:GetService("ReplicatedStorage")
local code1 = "YEET1"
script.Parent.MouseButton1Click:Connect(function()
	local input = script.Parent.Parent.Parent.CodeEnter.TextBox.Text
	if input ~= "" or nil then
		if input == code1 then
			RS.TwitterCode:FireServer(input)

The input here is blue like it is some keyword, in studio it doesn’t show it like this.

1 Like

oh roblox uses input as keyword in its modified version of lua so you cant use it as a variable try changing ‘input’ to Input, that is with a capital I in both your local and client scripts. Also in your server side script you need to change your structuring of if statements. do it like this instead:

if Codes[Input] then -- first you need to check if the code is valid so you are checking if a index exists in the table
      if Codes[Input].Used == false then
        ---then you do this
      end      
end

Also what you can do instead of structuring your code system like that is to just store a list of codes that the player already uses and a module script on the server with a list of valid codes. This will help to reduce the size of the data your are storing significantly.

1 Like

Okay, will try to do it. Later. Thanks ;).

Okay, so I did this and it didn’t work, no errors, meaning the if Codes[Input] then
stops it.

Can it be because of the ["YEET1"] speech marks?

It shouldn’t because you cannot do [YEET1]…

On the serverside of the code, try print the ‘input’ parameter to see if it is printing the result you want.

It does print the right thing.

Is ‘Codes’ nil by any chance at all?

It shouldn’t be nil, I’m getting it from DS2.

EDIT: I tried printing it and it looks like it is nil, no idea why.

Put a print after you check ‘if Codes[input] then’ and see if it is running.
If not then ‘Codes’ is probably nil

1 Like

try changing the common ‘i’ in input to Input

1 Like

I don’t think that would be the issue, I’ve tested in studio and it doesn’t seem to be recoloured by studio and stays the same regular colour as any other variable name.

There’s problem with the DataStore returning it nil, even though I set the value few lines above.

Try this:

local players = game:GetService("Players")
local dataStore2 = require(1936396537)
local CodeStoreName = "TwitterCodes"
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local function setupUserData()
	local Codes = {
		["YEET1"] = {Used = false, Reward = 500}
		}
	return Codes
end

ReplicatedStorage.TwitterCode.OnServerEvent:Connect(function(player, input)
local Codes = dataStore2(CodeStoreName, player):Get(setupUserData(), setupUserData()) --set a defualt value if there's no save 
	if Codes[input] and not Codes[input].Used then --if input is in codes & input is false then
		--do your code stuff here
	end
end)

wouldnt this generate a error tho? and by the way it is very bad to require modules like that

How so?
Also we’re currently settling it in dms.

Its just better to have them in game because if the developer of that module changes anything incorrectly when updating it it will affect your game

True, but when he fixes it, it will update.
Automatically.

AssetIds don’t change, and you get the new updates first too rather than having to change your current module in your game to the new one.

1 Like

i am not talking about the asset id

Anyhow, I wouldn’t think that kampfkarren would release a faulty version of their Module - I acually doubt any developer would.