Can't seem to pull more than one string from a datastore

Hello! This is my first post on the developer forum, I haven’t had trouble putting in multiple strings into a datastore (I used print to find this out) but I cannot pull more than one out.

Please don’t try to tell me my system sucks I like it the way it is

What the system does

I have a Star Wars game, I use lightsabers made by SolarHorizon, people can choose whatever color they want, which is why I made a system that only allows them to use certain colors, with boolvalues.

image

A player selects from one of these,

and fires to the server using the following:

local function Select()
	for i, v in next, svals:GetChildren() do
		if v.Value == true then
			event:FireServer(tostring(v.Name))
			source:Destroy()
			print(v)
		end	
	end
end

source.Fire.MouseButton1Click:Connect(Select)

(When a person selects a button the “sval” with the same name as the button becomes true

and then this is the server side

local function addCrystal(plr, info)
	datastore:SetAsync(plr.UserId, info)
	plr.Settings[info].Value = true
end

local function AddCurrents(plr)
	for _, v in next, plr.Settings:GetChildren() do
		local data = datastore:GetAsync(plr.UserId)
		if v.Name == tostring(data) then
			v.Value = true
		end
	end
end

When I try to use all of the colors I selected, only one of the values is true. Anyone know what the problem is besides me having terrible scripting abilities?

@ImTheBuildGuy this is the undeleted post xD

2 Likes

this is because you overwrite each of the colors each time you select one. :SetAsync sets data and doesn’t add onto it.

I tried doing

if datastore:GetAsync(plr.UserId) == nil then
    datastore:SetAsync(plr.UserId, info)
else
    datastore:UpdateAsync(plr.UserId, info)
end

and that didn’t work

try doing something like this for the for i,v in next script:

local function Select()
	local info = {}
	for i, v in next, svals:GetChildren() do
		if v.Value == true then
			table.insert(info, tostring(v.Name))
			source:Destroy()
			print(v)
		end	
	end
	event:FireServer(info)
end

source.Fire.MouseButton1Click:Connect(Select)
1 Like

What changes would I have to do in the server script, sorry if I am annoying you.

the server script should be fine it was just that you were individually saving them.

ServerScriptService.CrystalServer:11: invalid argument #2 (string expected, got table)

oh, change the server scripts AddCrystal function to this:

local function addCrystal(plr, info)
	datastore:SetAsync(plr.UserId, info)
	for i,v in pairs(info) do
		plr.Settings[v].Value = true
	end
end
1 Like

I owe you my life, and the ten hours it would have taken me to do this.