What this script does
Turn missing, malformed, or oversized saved data into a small predictable table before the rest of your server uses it.
- Best for
- Validating DataStore results and migration inputs in a pet or progression prototype.
- Language
- Luau
- Version
- 1.0.0
- Validation
- Luau runtime tested
Official Luau 0.735 parsing and strict analysis pass, and the module is covered by the pure-module runtime behavior harness.
Included behavior
- Applies documented defaults
- Clamps numeric ranges
- Limits string size and equipped-list length
Install in the stated runtime
- 1Create a ModuleScript named DataSchema.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Run sanitize immediately after loading data and before replicating any approved fields to the client.
Minimal usage example
local DataSchema = require(script.Parent.DataSchema)
local clean = DataSchema.sanitize(loadedValue)
print(clean.coins, clean.speed, #clean.equippedPetIds)Example inputs and result
- Supply
- A possibly incomplete or malformed saved player-data value.
- Expect
- sanitize returns a bounded record with the expected fields and defaults.
These notes explain the example above. Read the complete source for every exported function and edge case before integrating it.
Complete source
No hidden import, minified block, or shortened loader.
--!strict
-- MIT License. Sanitizes untrusted saved data into a small documented schema.
export type PlayerData = { coins: number, speed: number, equippedPetIds: { string } }
local DataSchema = {}
local defaults: PlayerData = { coins = 0, speed = 0, equippedPetIds = {} }
local function finiteNumber(value: unknown, minimum: number, maximum: number): number
if type(value) ~= "number" or value ~= value then return minimum end
return math.clamp(value, minimum, maximum)
end
function DataSchema.sanitize(value: unknown): PlayerData
if type(value) ~= "table" then return table.clone(defaults) end
local source = value :: { [any]: any }
local equipped = {}
if type(source.equippedPetIds) == "table" then
for _, id in source.equippedPetIds do
if type(id) == "string" and #id <= 64 and #equipped < 20 then table.insert(equipped, id) end
end
end
return {
coins = finiteNumber(source.coins, 0, 1e15),
speed = finiteNumber(source.speed, 0, 1e12),
equippedPetIds = equipped,
}
end
function DataSchema.defaults(): PlayerData return table.clone(defaults) end
return table.freeze(DataSchema)
de8ed2f1bab32c48ab6caa63438d22f2e82e640cc024d3edf720ef4fb7426714Outside this script’s scope
- Save or load DataStores
- Trust arbitrary extra fields
Found a problem or a better approach?
Tell us what failed, what you expected, and whether the issue happened in the Luau CLI or Roblox Studio. Suggestions for clearer types, safer server checks, or a more useful API are welcome.