local folder = script.Parent.Second_Stats:GetChildren()
local gems = folder.Gems
local money = folder.Money
local config = game.Workspace.Main.Configurations
local sett = require(config)
local DataStoreservice = game:GetService("DataStoreService")
local ds = DataStoreservice:GetDataStore(sett.datastoreName)
game.Players.PlayerAdded:Connect(function(player)
local loadedds = ds:GetAsync(player.UserId)
local Values = script.PlayerStats:Clone()
Values.Parent = player
if loadedds ~= nil then
else
gems.Value = 0
money.Value = 0
end
end)
If you could also help w/ PlayerRemoving as well that would help a ton!
I’ll add comments stating any error I find. I don’t know how the game’s hierarchy is so I can’t really say much about typing/spelling mistakes:
local folder = script.Parent.Second_Stats:GetChildren() -- You're using :GetChildren() here. This means that the 'folder' variable will just return an array of all its children instead of itself
local gems = folder.Gems -- You can't get the gems from the array of children
local money = folder.Money -- Same thing here
local config = game.Workspace.Main.Configurations -- minor detail, but you can just use 'workspace' instead of 'game.Workspace'
local sett = require(config)
local DataStoreservice = game:GetService("DataStoreService")
local ds = DataStoreservice:GetDataStore(sett.datastoreName) -- I'm not sure what does this mean. I'm guessing the 'datastoreName' is a function of the 'config' module which returns a string (so the name of the datastore you're trying to get)
game.Players.PlayerAdded:Connect(function(player)
local loadedds = ds:GetAsync(player.UserId)
local Values = script.PlayerStats:Clone()
Values.Parent = player
if loadedds ~= nil then
else
gems.Value = 0
money.Value = 0
end
end)
You need to update the DataStore everytime the player leaves. It’s really easy. Are you trying to set both gems and money values in the same datastore or in separate ones?
I understand everything in the script. I made the first 3 lines. The rest was made by a friend but he is not that advanced in datastores.
the part i need help most is this
if loadedds ~= nil then
else
gems.Value = 0
money.Value = 0
end
In the unedited version this is the comment he put in
if loadedds ~= nil then
-- put in values here
else
-- put in values here (THIS HAPPENDS IF A DATASTORE DOESNT EXIST
end
Use :UpdateAsync() under a PlayerRemoving event. However there are 2 values, so either you create two separate datastores, one for the gems and one for the money, or you wrap the money and gems values inside a table and save the table instead.
game.Players.PlayerRemoving:Connect(function(Player)
ds:UpdateAsync(Player.UserId, function(Old)
local New = Old or {}
New = {["Gems"] = gems.Value; ["Money"] = money.Value;}
return New
end
end)