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.

local DS1 = game:GetService("DataStoreService"):GetDataStore("Coins")
local DS2 = game:GetService("DataStoreService"):GetDataStore("Gems")
local DS3 = game:GetService("DataStoreService"):GetDataStore("equippedKnife")
local DS4 = game:GetService("DataStoreService"):GetDataStore("equippedGun")

game.Players.PlayerAdded:connect(function(player)

    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local coins = Instance.new("IntValue", stats)
    coins.Name = "Coins"
    coins.Value = DS1:GetAsync(player.UserId)

    local gems = Instance.new("IntValue", stats)
    gems.Name = "Gems"
    gems.Value = DS2:GetAsync(player.UserId)

    local inGame = Instance.new("BoolValue", player)
    inGame.Name = "InGame"

    local equippedKnife = Instance.new ("StringValue", player)
    equippedKnife.Name = "equippedKnife"
    equippedKnife.Value = "Default"

    local equippedGun = Instance.new("StringValue", player)
    equippedGun.Name = "equippedGun"
    equippedGun.Value = "Default"

    while wait(60) do
        DS1:SetAsync(player.UserId.."_DS1", coins.Value)
        print("Saved Coins")
        --Saved Coins
        DS2:SetAsync(player.UserId.."_DS2", gems.Value)
        print ("Saved Gems")
        --Saved Gems
        DS3:SetAsync(player.UserId.."_DS3", equippedKnife.Value)
        print ("Saved knifeEquipped")
        --Saved equipped Knife
        DS4:SetAsync(player.UserId.."_DS4", equippedGun.Value)
        print ("Saved gunEquipped")
        --Saved equipped Gun
      end
end)

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".

local DS1 = game:GetService("DataStoreService"):GetDataStore("Coins")
local DS2 = game:GetService("DataStoreService"):GetDataStore("Gems")
local DS3 = game:GetService("DataStoreService"):GetDataStore("equippedKnife")
local DS4 = game:GetService("DataStoreService"):GetDataStore("equippedGun")

game.Players.PlayerAdded:connect(function(player)
    local id = player.UserId;

    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local coins = Instance.new("IntValue", stats)
    coins.Name = "Coins"
    coins.Value = DS1:GetAsync(id.."_DS1") --// You need to change this to be the same as the key you saved it as.

    local gems = Instance.new("IntValue", stats)
    gems.Name = "Gems"
    gems.Value = DS2:GetAsync(id.."_DS2") --// Same with this.

    local inGame = Instance.new("BoolValue", player)
    inGame.Name = "InGame"

    local equippedKnife = Instance.new ("StringValue", player)
    equippedKnife.Name = "equippedKnife"
    equippedKnife.Value = DS3:GetAsync(id.."_DS3") or "Default" --// Get the value, and fallback to "Default" if it doesn't exist.

    local equippedGun = Instance.new("StringValue", player)
    equippedGun.Name = "equippedGun"
    equippedGun.Value = DS4:GetAsync(id.."_DS4") or "Default" --// Same as above.

    while wait(60) do
        DS1:SetAsync(player.UserId.."_DS1", coins.Value)
        print("Saved Coins")
        --Saved Coins
        DS2:SetAsync(player.UserId.."_DS2", gems.Value)
        print ("Saved Gems")
        --Saved Gems
        DS3:SetAsync(player.UserId.."_DS3", equippedKnife.Value)
        print ("Saved knifeEquipped")
        --Saved equipped Knife
        DS4:SetAsync(player.UserId.."_DS4", equippedGun.Value)
        print ("Saved gunEquipped")
        --Saved equipped Gun
      end
end)

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