Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to set a new players data to something?

Asked by
jdc892 4
7 years ago
Edited 7 years ago

I can load and save my coins and gems, but I want to set "equippedGun" and "equippedKnife" to "Default" if they are new to my game. How would I do that? NOTE: this is ONLY if they are NEW to my game, not just everytime they join, so if they have played before it would do the one they had Here is what I have so far.

01local DS1 = game:GetService("DataStoreService"):GetDataStore("Coins")
02local DS2 = game:GetService("DataStoreService"):GetDataStore("Gems")
03local DS3 = game:GetService("DataStoreService"):GetDataStore("equippedKnife")
04local DS4 = game:GetService("DataStoreService"):GetDataStore("equippedGun")
05 
06game.Players.PlayerAdded:connect(function(player)
07 
08    local stats = Instance.new("IntValue", player)
09    stats.Name = "leaderstats"
10 
11    local coins = Instance.new("IntValue", stats)
12    coins.Name = "Coins"
13    coins.Value = DS1:GetAsync(player.UserId)
14 
15    local gems = Instance.new("IntValue", stats)
View all 44 lines...

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

All you have to do is use the same thing you used for coins and gems. You need to use GetAsync in order to get the values, and check if they've been set.

As the wiki states, "If the key does not exist, [GetAsync] returns nil." Using this information, you can determine whether or not they've played the game before, and set it to either the saved value, or "Default".

01local DS1 = game:GetService("DataStoreService"):GetDataStore("Coins")
02local DS2 = game:GetService("DataStoreService"):GetDataStore("Gems")
03local DS3 = game:GetService("DataStoreService"):GetDataStore("equippedKnife")
04local DS4 = game:GetService("DataStoreService"):GetDataStore("equippedGun")
05 
06game.Players.PlayerAdded:connect(function(player)
07    local id = player.UserId;
08 
09    local stats = Instance.new("IntValue", player)
10    stats.Name = "leaderstats"
11 
12    local coins = Instance.new("IntValue", stats)
13    coins.Name = "Coins"
14    coins.Value = DS1:GetAsync(id.."_DS1") --// You need to change this to be the same as the key you saved it as.
15 
View all 45 lines...

You may want to look into using a table to save values as that may be a bit neater, but this way would work fine as well.
Hope this helped.

0
Thanks! This is exactly what I was looking for! Since I am only a beginner scripter and have not learnt this stuff it is hard for me. Thanks again! jdc892 4 — 7y
0
No problem. Pyrondon 2089 — 7y
Ad

Answer this question