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

Issue with strings and a table..?

Okay so I have this table:

local defaultdatas = {
	Enchantments = 0
	Slot1 = 0,
	Enchantments1 = 0,
	Slot2 = 0,
	Enchantments2 = 0,
	Slot3 = 0,
	Enchantments3 = 0,
	Slot4 = 0,
	Enchantments4 = 0,
	Slot5 = 0,
	Enchantments5 = 0
	
}

Then this line of code

local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local ds = DataStore:GetDataStore("Playerdata")
game.Players.PlayerAdded:connect(function(player)
    local datas = ds:GetAsync(player.UserId)
if datas then	
    datas = HttpService:JSONDecode(datas)
else
	datas = defaultdatas
	end
    
    local data = Instance.new("Folder",player)
    data.Name = "Data"
local numberofslots = 5
local slotcurrentlyat = 1
for i = 1, numberofslots do
	local slot = Instance.new("StringValue", data)
	slot.Name = "Slot"..slotcurrentlyat
	slot.Value = data["Slot"..slotcurrentlyat] or defaultdatas["Slot"..slotcurrentlyat]
	local enchantments = Instance.new("StringValue", data)
	enchantments.Name = "Enchantments"..slotcurrentlyat
	enchantments.Value = data["Enchantments"..slotcurrentlyat] or defaultdatas["Enchantments"..slotcurrentlyat]
	slotcurrentlyat = slotcurrentlyat + 1
	wait()
	end
end)

Apparently the only issues are:

enchantments.Value = data["Enchantments"..slotcurrentlyat] or defaultdatas["Enchantments"..slotcurrentlyat]

and

slot.Value = data["Slot"..slotcurrentlyat] or defaultdatas["Slot"..slotcurrentlyat]

also this error

ServerScriptService.Data:19: bad argument #3 to 'Value' (string expected, got Object)

obv this isn't the whole script, but these are the only parts linked to the issue I have. Please help.

1 answer

✓ Accepted answer

The problem is very simple here. The variable data is created on line 12.

local data = Instance.new("Folder",player) 

This means that data["Slot"..slotcurrentlyat] will be using the folder instance. The second part is on like 17 when you create.

local slot = Instance.new("StringValue", data) -- see how the parent is data???
slot.Name = "Slot"..slotcurrentlyat -- you then set the name

You then go on to use.

slot.Value = data["Slot"..slotcurrentlyat] or defaultdatas["Slot"..slotcurrentlyat]
-- the above code is the same as
slot.Value = slot -- ?????

This will case it to error as you are passing the StringValue as the value type?

I have no idea what you are using this code for but you should be able to solve this on your own.

I hope this helps. Please comment if you have any other questions.

3 comments

0

ty for the answer but I misspelled data with datas in the part where I set the slot value

0

Yep I was looking through it again and I did see that after. In general try to stay away from using plural variable names.

0

Or limit the scope of variables by using do end blocks

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