local DataStoreService = game:GetService("DataStoreService") --Get Datastore
local MainDatastore = DataStoreService:GetDataStore("MainDatastore") --Instead of having multiple datastores, why not use one!
local defaultData = {}
local DataTable = {}
for index, value in pairs(script:GetChildren()) do
defaultData[value.Name] = value.Value --Creating a preset table
end
game.Players.PlayerAdded:Connect(function(player) --Fetch the player
local fetchedData
local success, errormessage = pcall(function()
fetchedData = MainDatastore:GetAsync(player.UserId) --Using a pcall (error-handling) to fetch it in case of an error
end)
if success then ---Check if successful
if fetchedData ~= nil then --Check if data is not nil
for Index, Value in pairs(fetchedData)do
local NewValue = Instance.new("NumberValue") --Instancing number value
NewValue.Name = Index --Setting name to the index of the fetched data
NewValue.Value = Value or defaultData[Index]
NewValue.Parent = player --Better to parent it later than as a second paramter
end
end
else
warn(errormessage) --Show us the error.
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(player:GetChildren()) do
if v:IsA("NumberValue") then
DataTable[v.Name] = v.Value
end
end
local success, errormessage = pcall(function()
MainDatastore:SetAsync(player.UserId, DataTable)
end)
end)
game:BindToClose(function()
local Players = game:GetService("Players")
for index, Player in pairs(Players:GetPlayers()) do
for i,v in pairs(Player:GetChildren()) do
if v:IsA("NumberValue") then
DataTable[v.Name] = v.Value
end
end
local success, errormessage = pcall(function()
MainDatastore:SetAsync(Player.UserId, DataTable)
end)
end
end)
Aim of this is to yield server closure for a bit, because I believe the server is shutting down before it can successfully save all the data, Just a theory though
I think the tutorial is from crazycorrs on youtube
called how to use datastore on roblox, easiest method. You dont have to use it, just see it for information
Of course this doesn’t work, why would it lol
Try This:
local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(player)
for i,v in pairs(script:GetChildren()) do
local d = DataStoreService:GetDataStore("DS" .. v.Name .. player.UserId)
local x = Instance.new("NumberValue", player)
x.Name = v.Name
x.Value = d:GetAsync(player.UserId) or v.Value
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(script:GetChildren()) do
print("Getting")
local d = DataStoreService:GetDataStore("DS" .. v.Name .. player.UserId)
d:SetAsync(player.UserId, player[v.Name].Value)
print("Saved")
end
end)
I literally have no idea. I’ll debug more thoroughly
This doesn’t work as well. I don’t get why nothing works.
Can you check again that you anabled api services
Any syntax highlights in the script?
What do you mean by any syntax highlights?
Red underlines above any code is what i mean
Nope, there isn’t any and no errors in the console.
Sorry apparently i thought the red lines below the code are syntax highlights
Im trying it in my studio now hold on
local DataStore2 = require(workspace.MainModule)
DataStore2.Combine("MasterKey", "Points")
local DefaultValue = 0
game.Players.PlayerAdded:Connect(function(player)
local PointsDatastore = DataStore2("Points", player)
local Points = Instance.new("IntValue", player)
Points.Name = "Points"
if PointsDatastore:Get() ~= nil then
Points.Value = PointsDatastore:Get()
else
Points.Value = DefaultValue
end
Points.Changed:Connect(function()
PointsDatastore:Set(Points.Value)
end)
end)
this should work, when setting value in player, make sure you are using a server script and not using a client script.
Take note, i inserted the module into workspace instead of requiring by id.
Also you should add a bool value into the serverstarage named “SaveInStudio” set to true. this will make sure the module saves while in studio.
I did all the things you said and it doesn’t even make a value.
Any errors or red underlines? Are you sure you insetted the datastore module in workspace?
There are no errors and I did put the module in workspace and it now makes a value but doesn’t save it.
Alright I am back, with a fully working version of it (hopefully)
local DataStoreService = game:GetService("DataStoreService") --Get Datastore
local MainDatastore = DataStoreService:GetDataStore("MainDatastore") --Instead of having multiple datastores, why not use one!
local defaultData = {}
local DataTable = {}
for index, value in pairs(script:GetChildren()) do
defaultData[value.Name] = value.Value --Creating a preset table
print("Preset")
end
local function PlayerAdded(player)
print("Boom")
local fetchedData
local success, errormessage = pcall(function()
fetchedData = MainDatastore:GetAsync(player.UserId) --Using a pcall (error-handling) to fetch it in case of an error
end)
if success then ---Check if successful
print("Success")
if fetchedData ~= nil then
print("Fetched Data Not Nil")
for Index, Value in pairs(fetchedData)do
local NewValue = Instance.new("NumberValue") --Instancing number value
NewValue.Name = Index --Setting name to the index of the fetched data
NewValue.Value = Value or defaultData[Index]
NewValue.Parent = player --Better to parent it later than as a second paramter
NewValue:GetPropertyChangedSignal("Value"):Connect(function()
DataTable[NewValue.Name] = NewValue.Value
print("Changed")
end)
end
else
print("Fetched Data is nil")
for index, value in pairs(defaultData) do
print("Currently On: "..index)
local Value = Instance.new("NumberValue")
Value.Name = index
Value.Parent = player
Value.Value = value
print("Made Value")
Value:GetPropertyChangedSignal("Value"):Connect(function()
DataTable[Value.Name] = Value.Value
print("Value changed")
end)
end
end
else
warn(errormessage) --Show us the error.
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(function(player)
print("Leaving")
for i,v in pairs(player:GetChildren()) do
if v:IsA("NumberValue") then
DataTable[v.Name] = v.Value
end
end
MainDatastore:SetAsync(player.UserId, DataTable)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
for i,v in pairs(player:GetChildren()) do
if v:IsA("NumberValue") then
DataTable[v.Name] = v.Value
end
end
MainDatastore:SetAsync(player.UserId, DataTable)
wait()
end
end)
for i, v in pairs(game.Players:GetPlayers()) do
coroutine.resume(coroutine.create(function()
PlayerAdded(v)
end))
end
You can remove the prints if you wish they were only for debugging
1 Like
Are you changing the value of the thing in a server script or on the server console?