thats a smart idea i think but its a little risky, lets see if we can preserve it first
if we can’t preserve it, what i would do is internally store your own version of jobids which you can guarantee to be unique to your game (you will have to guarantee this of course), in this case you could store the job id as 5 characters because i assume your game will not be popular enough for (36)^5 servers
local function serialize(plrs)
local buff=bitbuffer()
buff:ushort(#plrs)
for _,t in next,plrs do
buff:ulong(t.userId)
local len=#t.jobId
buff:uint(len)
for i=1,len do
buff:alphanum(t.jobId:sub(i,i))
end
end
return buff:ds()
end
local function deserialize(s)
local buff=bitbuffer.ds(s)
local plrs={}
for i=1,buff:ushort()do
local uid=buff:ulong()
local len=buff:uint()
local job=''
for i=1,len do
job=job..buff:alphanum()
end
plrs[i]={
userId=uid,
username=game:GetService'Players':GetNameFromUserIdAsync(uid),
jobId=job,
}
end
end
dont run strings through bitbuffers, that is really bad
_G.reload()
local bitbuffer=_G.bitbuffer
local function serialize(plrs)
local buff=bitbuffer()
buff:ushort(#plrs)
for _,t in next,plrs do
buff:ulong(t.userId)
local len=#t.jobId
buff:uint(len)
for i=1,len do
buff:alphanum(t.jobId:sub(i,i))
end
end
return buff:dump()
end
local function deserialize(s)
local buff=bitbuffer(s)
local plrs={}
for i=1,buff:ushort()do
local uid=buff:ulong()
local len=buff:uint()
local job=''
for i=1,len do
job=job..buff:alphanum()
end
plrs[i]={
userId=uid,
username=game:GetService'Players':GetNameFromUserIdAsync(uid),
jobId=job,
}
end
end
local plrs={}
for i=1,22 do
plrs[i]={userId=113926330,username='CoreDev',jobId='test3483fh3487fgw983rte497t'}
end
print(#serialize(plrs))
print(#_G.http:JSONEncode(plrs))
22 players with bitbuffer is:
682
json is:
1761
but I’m using all 256 characters now not datastore friendly so I’m not sure if this works for you?
I don’t care about datastores that much (I have my own data storing solution) and the only thing that matters is getting this message compressed enough that I can get it under 1kb. I need to be able to store 50 players inside of this (at max).
_G.reload()
local bitbuffer=_G.bitbuffer
local function serialize(plrs)
local buff=bitbuffer()
buff:ushort(#plrs)
local pos={}
for i=1,10 do
pos[i]=math.random()
end
local function ulong(job)
local x=0
for i=1,10 do
local c=math.floor(pos[i]*#job)+1
c=job:sub(c)
local n=tonumber(c)
n=n or string.byte(c)-string.byte'a'+10
x=x*36+n
end
return x
end
for _,t in next,plrs do
buff:ulong(t.userId)
buff:ulong(ulong(t.jobId))
end
return buff:dump()
end
local function deserialize(s)
local buff=bitbuffer(s)
local plrs={}
for i=1,buff:ushort()do
local uid=buff:ulong()
plrs[i]={
userId=uid,
username=game:GetService'Players':GetNameFromUserIdAsync(uid),
jobId=buff:ulong()..'',
}
end
return plrs
end
local plrs={}
for i=1,50 do
plrs[i]={userId=113926330,username='CoreDev',jobId='test3483fh3487fgw983rte497t'}
end
print(#serialize(plrs))
print(#_G.http:JSONEncode(plrs))
its 665 characters
it does something similar to taking the 5 characters you mentioned, but instead it takes 10 (because that is just enough to fit in an integer double w/o losing precision) random characters but that are the same for all of the job ids in this serialize call that have the same length (if they have different lengths the characters taken will be different, nothing to do about that unless we take FIRST five characters but I would rather not trust that Roblox’s job ids are completely random)
using these new hashed job ids, you are still guaranteed that players who had the same job ids before still have the same job ids
the only downside is that with astronomically low probability players in separate servers may be said to have the same job id
userids are optimally and losslessly stored
here it is w/o username (removed one line, didnt affect the size)
_G.reload()
local bitbuffer=_G.bitbuffer
local function serialize(plrs)
local buff=bitbuffer()
buff:ushort(#plrs)
local pos={}
for i=1,10 do
pos[i]=math.random()
end
local function ulong(job)
local x=0
for i=1,10 do
local c=math.floor(pos[i]*#job)+1
c=job:sub(c)
local n=tonumber(c)
n=n or string.byte(c)-string.byte'a'+10
x=x*36+n
end
return x
end
for _,t in next,plrs do
buff:ulong(t.userId)
buff:ulong(ulong(t.jobId))
end
return buff:dump()
end
local function deserialize(s)
local buff=bitbuffer(s)
local plrs={}
for i=1,buff:ushort()do
local uid=buff:ulong()
plrs[i]={
userId=uid,
jobId=buff:ulong()..'',
}
end
return plrs
end
local plrs={}
for i=1,50 do
plrs[i]={userId=113926330,username='CoreDev',jobId='test3483fh3487fgw983rte497t'}
end
print(#serialize(plrs))
print(#_G.http:JSONEncode(plrs))
_G.reload()
local bitbuffer=_G.bitbuffer
local function serialize(plrs)
local buff=bitbuffer.new()
buff:ushort(#plrs)
local pos={}
for i=1,10 do
pos[i]=math.random()
end
local function ulong(job)
local x=0
for i=1,10 do
local c=math.floor(pos[i]*#job)+1
c=job:sub(c)
local n=tonumber(c)
n=n or string.byte(c)-string.byte'a'+10
x=x*36+n
end
return x
end
for _,t in next,plrs do
buff:ulong(t.userId)
buff:ulong(ulong(t.jobId))
end
return buff:dump()
end
local function deserialize(s)
local buff=bitbuffer.new(s)
local plrs={}
for i=1,buff:ushort()do
local uid=buff:ulong()
plrs[i]={
userId=uid,
jobId=buff:ulong()..'',
}
end
return plrs
end
local plrs={}
for i=1,50 do
plrs[i]={userId=113926330,jobId='test3483fh3487fgw983rte497t'}
end
print(#serialize(plrs))
print(#_G.http:JSONEncode(plrs))
in my framework I have the tables have the module tables have __call linked to their .new constructor
In case the code above errors because messagingservice doesn’t support all 256 characters, use this version (its 824 characters):
_G.reload()
local bitbuffer=_G.bitbuffer
local function serialize(plrs)
local buff=bitbuffer.new()
buff:ushort(#plrs)
local pos={}
for i=1,10 do
pos[i]=math.random()
end
local function ulong(job)
local x=0
for i=1,10 do
local c=math.floor(pos[i]*#job)+1
c=job:sub(c)
local n=tonumber(c)
n=n or string.byte(c)-string.byte'a'+10
x=x*36+n
end
return x
end
for _,t in next,plrs do
buff:ulong(t.userId)
buff:ulong(ulong(t.jobId))
end
return buff:ds()
end
local function deserialize(s)
local buff=bitbuffer.ds(s)
local plrs={}
for i=1,buff:ushort()do
local uid=buff:ulong()
plrs[i]={
userId=uid,
jobId=buff:ulong()..'',
}
end
return plrs
end
local plrs={}
for i=1,50 do
plrs[i]={userId=113926330,jobId='test3483fh3487fgw983rte497t'}
end
print(#serialize(plrs))
print(#_G.http:JSONEncode(plrs))