Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I store NumberValue to database???

Asked by 2 years ago

I have a value stored in replicatedstore, how do I save it to database so when I rejoin, the number stays there and doesn't reset??

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Use DataStore Service.

I'll show you a little example of it using a number value(you can change the number value with any number value in the game, it could be a number that is in the leaderstats folder, if you have one).

local saveData = game:GetService('DataStoreService'):GetDataStore('NumberSaver') 

local number -- type here the location of the number value.

local plrs = game:GetService('Players')

plrs.PlayerAdded:Connect(function(plr)

local newNumber
local s, e = pcall(function()
newNumber = saveData:GetAsync(plr.UserId) -- gets number from id
end)
if s then
  if newNumber == nil then
   number.Value = 0 -- put here the number that you want when there is a new player in the game.
  else -- if the player already joined the experiece before, it will give the number value the same value that it had before
   number.Value = newNumber
  end
else
  warn(e) -- if there is a error, it will send it to the output
end
end) -- when the player joins the server, the data store will get the number in the user id of that player and put it in number.Value.

plrs.PlayerRemoving:Connect(function(plr)
local s, e = pcall(function()
 saveData:SetAsync(plr.UserId, number)
end)
if  not s then
  warn(e) -- if there is a error, it will send it to the output
end
end) -- when the player leaves the server, the data store will save the number in the user id of that player.

This will change the value of 'number' when player joins and leaves.

0
104: Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters. Shadrocks12345 0 — 2y
0
thats the error I got Shadrocks12345 0 — 2y
0
at line 26, add .Value to 'number', I forgot to put that in there. Sarturex857 109 — 2y
0
ive tried that and this is what I got lol ServerScriptService.Database:15: attempt to index number with 'Value' Shadrocks12345 0 — 2y
0
what did you put in number? Sarturex857 109 — 2y
Ad

Answer this question