You are loading the data on line 59, but you don't actually read from it. You should rename the variable dataExists
to data
; after lines 70, 76, and 82, you should then have if data[v] then value.Value = data[v] end
Further, you can't save Color3 values in the datastore directly. You will need serialize/deserialize functions (put these before you use them):
1 | local function serializeColor 3 (c) |
2 | return { R = c.R, G = c.G, B = c.B } |
4 | local function deserializeColor 3 (data) |
5 | return Color 3. new(data.R, data.G, data.B) |
Call serializeColor3
whenever you want to save a Color3 and deserializeColor3 whenever you want to load one. Line 172 can become: storage[v.Name] = typeof(v.Value) == "Color3" and serializeColor3(v.Value) or v.Value
and after line 82 becomes if data[v] then value.Value = deserializeColor3(data[v]) end
Another mild concern is updating data. What happens if you later change what you want stored in the profile? I would assume you would want to use the data loaded where possible and use default values for the rest. You can do this by checking to see if 'data' has the desired key for each value (and ensuring that 'data' is at least an empty table so you don't need to check that each time). ex, instead of line 63 onward, you have:
04 | local random = Random.new(tick()) |
06 | playerFolder.Age.Value = agesTable [ random:NextInteger( 1 , #agesTable) ] |
09 | if not data.Clan or data.Clan = = "" then |
10 | playerFolder.Clan.Value = possibleClans [ random:NextInteger( 1 , #possibleClans) ] |
12 | playerFolder.Clan.Value = data.Clan |
Also, if there is no "data" (you check if there is data on line 170), you probably shouldn't save anything - you'd be overwriting anything they had previously. (This is not responsible for your problems, but is a good idea to fix in case something goes wrong.)
Other notes:
- you don't need parentheses around an if statement's condition in Lua.
- no need for a
wait()
line 186
- if
fetch
is false, something has gone wrong - you should consider waiting a few seconds and trying again before telling the user you couldn't load their data. If this happens, you probably shouldn't save their data later, either - you would overwrite their earlier save (if they had any) - you could ask the user what they want to do.
- You've just created the values, so you don't need
FindFirstChild
when you're loading in default data (and there's no point in using FindFirstChild if you aren't going to check to see if a child was found anyway). ex, instead of playerFolder:FindFirstChild("Age").Value
, just say playerFolder.Age.Value