|
| 16 Nov 2014 05:40 PM |
table = {["Kills"] = 0,["Level"] = 1}
How do I create leaderstats based on these names with these default values? |
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 16 Nov 2014 05:45 PM |
local stats = Instance.new("IntValue") stats.Name = "leaderstats"
local kills = Instance.new("IntValue") kills.Value = data.Kills or 0 --if data.Kills is nil it'll give you zero
local level = Instance.new("IntValue") level.Value = data.Level or 1
Just load the data with GetAsync like we talked about earlier. |
|
|
| Report Abuse |
|
|
|
| 16 Nov 2014 05:56 PM |
OK let me make this clear as possible, so we both understand what I'm talking about
DataStore name = PlayerStats Scope = Player User ID Key = Statname? or Table?
1. Player enters the game, a table gets created. Or table is created beforehand in the script, like Stats = {"Kills, "Deaths", "Streak"}
2. GetAsync collects values inside the player's leaderstats. It looks for names from the tables, and then the player's leaderstats, right?
3. UpdateAsync the player's stats in a table, how do I do that?
4. Some values start on different numbers, other than 0 when the player plays the game for the first time. (Levels)
What do I do?
|
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 16 Nov 2014 06:26 PM |
local dataStore = game:GetService("DataStoreService"):GetDataStore("Data") game.Players.PlayerAdded:connect(function(player) local data = dataStore:GetAsync(tostring(player.userId)) or {}
local stats = Instance.new("IntValue") stats.Name = "leaderstats" local kills = Instance.new("IntValue", stats) kills.Name = "Kills" kills.Value = data.Kills or 0
local level = Instance.new("IntValue", stats) level.Name = "Level" level.Value = data.Level or 1
local streak = Instance.new("IntValue", stats) streak.Name = "Streak" streak.Value = data.Streak or 0
stats.Parent = player end)
game.Players.PlayerRemoving:connect(function(player) dataStore:UpdateAsync(tostring(player.userId), function() return { Kills = player.Kills.Value, Level = player.Level.Value, Streak = player.Streak.Value } end) end)
I'm pretty sure this is what you wanted for your leaderboard script. Hopefully it makes sense to you now that you see it. |
|
|
| Report Abuse |
|
|
|
| 16 Nov 2014 06:47 PM |
| This makes perfect sense! but one thing, what is 'tostring' for? |
|
|
| Report Abuse |
|
|
|
| 16 Nov 2014 06:49 PM |
tostring can make any other type of value into a string. i.e. tostring(1) is the same as "1" |
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 16 Nov 2014 06:53 PM |
I just do it as good convention, you might be able to get away with not using it in here.
So if we take the number 2103, we can do math on it, store it within three bytes, etc. In some other languages you might not be able to concatenate it or anything like that so we turn the number 2103 into a string that holds the text 2103.
The keys for data stores take strings. userId returns a number. Like I just said, you might be able to get away with not converting it to a string (it'll be done automatically if so), but I like to be thorough, and it should be clear to everyone reading it that that argument is a string.
You can also convert strings to numbers with tonumber() |
|
|
| Report Abuse |
|
|
|
| 16 Nov 2014 06:59 PM |
| I see. Also thank you so much for helping me! Sorry if I repeated my questions many times. ONE MORE OBVIOUS CLARIFICATION, this surely only takes 2 requests, one for joining and one for leaving? |
|
|
| Report Abuse |
|
|