generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Creating types of values based on tables

Previous Thread :: Next Thread 
Tedgary009 is not online. Tedgary009
Joined: 29 Dec 2009
Total Posts: 339
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 is not online. 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
Tedgary009 is not online. Tedgary009
Joined: 29 Dec 2009
Total Posts: 339
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 is not online. 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
Tedgary009 is not online. Tedgary009
Joined: 29 Dec 2009
Total Posts: 339
16 Nov 2014 06:47 PM
This makes perfect sense! but one thing, what is 'tostring' for?
Report Abuse
Eternalfireeater is not online. Eternalfireeater
Joined: 01 May 2011
Total Posts: 10027
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 is not online. 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
Tedgary009 is not online. Tedgary009
Joined: 29 Dec 2009
Total Posts: 339
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image