|
-------Read the bottom of the script
local DataStore = game:GetService("DataStoreService"):GetDataStore("DerfStore")
game.Players.PlayerAdded:connect(function(player)
local stats = Instances.new("IntValue", player)
stats.Name = "leaderstats"
local gold = Instances.new("IntValue", stats)
gold.Name = "gold"
local level = Instances.new("IntValue", stats)
level.Name = "level"
local EXP = Instances.new("IntValue", stats)
EXP.Name = "Exp"
local key = "player-"..player.userId
local savedValues = DataStore:GetAsync(key)
if savedValues == nil then
--Save format: (Gold, level, exp)
gold.value = savedValues(1)
level.value = savedValues(2)
EXP.value = savedValues(5)
else
local valuesToSave = {gold.value, level.value, EXP.value}
DataStore:setAsync(key, valuesToSave)
end
end)
-------for some reason the leaderboard doesnt appear in game, can someone help me out?
Online dancing since 1337 |
|
|
Bump
Online dancing since 1337 |
|
|
Bump[2]
Online dancing since 1337 |
|
|
Bump[3]
Online dancing since 1337 |
|
|
Bump[4]
Online dancing since 1337 |
|
|
Bump[5]
Online dancing since 1337 |
|
|
Bump[6]
Online dancing since 1337 |
|
|
Bump[7]
Online dancing since 1337 |
|
|
I believe your leaderstats should be a model
so it should be like this
-Players
--Player
---leaderstats(model)
----Gold
----Otherthinginleaderboard
----Cash
----Otherthinginleaderboard2 |
|
|
So change this...
local stats = Instances.new("IntValue", player)
stats.Name = "leaderstats"
to this....
local stats = Instances.new("Model", player)
stats.Name = "leaderstats" |
|
instawinJoin Date: 2013-06-04 Post Count: 8777 |
there's some things that are wrong:
you misspelled the Instance table, it is Instance.new(classname, parent), not Instances.new(classname, parent)
methods are case sensitive (eg: not :setAsync(key, val) but :SetAsync(key, val))
you used the wrong brackets to index from a table, use these: [ ]
properties are case sensitive (eg: not .value, but .Value)
your script wasn't saving data when you left
why were you trying to set the values of your stats to values of your savedValues table, if savedValues is nil?
and you may want to use game.OnClose for saving data when the player leaves
local DataStore = game:GetService("DataStoreService"):GetDataStore("DerfStore")
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("Folder", player)
stats.Name = "leaderstats"
local gold = Instance.new("IntValue", stats)
gold.Name = "gold"
local level = Instance.new("IntValue", stats)
level.Name = "level"
local EXP = Instance.new("IntValue", stats)
EXP.Name = "Exp"
local key = "player-"..player.userId
local savedValues = DataStore:GetAsync(key)
if savedValues then
--Save format: (Gold, level, exp)
gold.Value = savedValues[1]
level.Value = savedValues[2]
EXP.Value = savedValues[3]
else
gold.Value = 0
level.Value = 0
EXP.Value = 0
end
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "player-"..plr.userId
local stats = plr:FindFirstChild("leaderstats")
local gold = stats:FindFirstChild("Gold")
local level = stats:FindFirstChild("Level")
local EXP = stats:FindFirstChild("Exp")
if stats and gold and level and EXP then
dataStore:SetAsync(key, {gold.Value, level.Value, EXP.Value})
end
end)
|
|
|
I realise that but wouldnt that only work IF the player left the game?
for example (if I shut the game down for an update it wouldnt save the data)
Online dancing since 1337 |
|
instawinJoin Date: 2013-06-04 Post Count: 8777 |
yes, that is why i suggested using game.OnClose
either way, your original script didn't even save data. |
|
kritikiJoin Date: 2012-07-12 Post Count: 12323 |
@OP
would it be alright if i copied your script?
i would give all credit to you to make sure
if its gonna save data that is |
|
instawinJoin Date: 2013-06-04 Post Count: 8777 |
actually no, because .PlayerRemoving fires regardless, even if the game is shut down, or he loses connection.
still, use game.OnClose |
|
ShungTzuJoin Date: 2014-06-14 Post Count: 959 |
Try
local key = ("player-"..tostring(player.userId)
local savedValues = DataStore:GetAsync(key)
if not savedValues then
print('no savedValues')
local valuesToSave = {}--gold.value, level.value, EXP.value
valuesToSave['Gold']=gold.Value
valuesToSave['Level']=level.Value
valuesToSave['EXP']=EXP.Value
DataStore:setAsync(key,valuesToSave)
else
print('values loaded from DataStore')
gold.Value=savedValues.gold
level.Value=savedValues.level
EXP.Value=savedValues.EXP
end
, and use prints to troubleshoot your own stuff. |
|
|
would this count as a "On leave save"?
game.Players.PlayerRemoving:connect(function(player)
local key = "player-"..player.userId
--Save key: {Gold, level, exp}
local valuesToSave = {players.leaderstats.gold.value, players.leaderstats.level.value, players.leaderstats.EXP.value, }
DataStore:SetAsync(key, valueToSave)
end)
I came across one of peaspods videos and looked at it, not sure if its outdated but he used it in a separate script would it work?
Online dancing since 1337 |
|
instawinJoin Date: 2013-06-04 Post Count: 8777 |
my modified version of your script would work for the most part, but there could be some data loss depending if leaderstats doesn't exist when the player leaves
use the game.OnClose callback
http://wiki.roblox.com/index.php?title=API:Class/DataModel/OnClose
peaspod's tutorial on datastores is still relevant. |
|