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

Adding Datastore Service?

Asked by 8 years ago

How do I add Datastore? I have an Leaderboard Script that I works Perfect but I really neeed a Datastore to save the Points. Thing is I have never done Datastore, so How do I add DataStore to this script?

This Is The Leaderboard Script

players = game:GetService('Players')
startingcash = 0
-- Variables

players.PlayerAdded:connect(function(plr)
local stats = Instance.new('IntValue')
local cash = Instance.new('IntValue')
stats.Name = 'leaderstats'
cash.Name = 'Points'
cash.Parent = stats
cash.Value = startingcash
stats.Parent = plr
local AddPointGui = script.Parent.Parent.StarterGui.HandtoGui.Accept.Yes
AddPointGui.MouseButton1Click:connect(function()
cash.Value = cash.Value + 1
end)
end)

1 answer

Log in to vote
0
Answered by 8 years ago

In a serverscript:

local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore")


game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local stat1 = Instance.new("IntValue")
    stat1.Name = "Points"
    stat1.Parent = leaderstats

    local key = "user-" .. player.userId

    local storeditems = datastore:GetAsync(key)
    if storeditems then
        stat1.Value = storeditems[1]
    else
        local items = {stat1.Value}
        datastore:SetAsync(key, items)
    end

end)

game.Players.PlayerRemoving:connect(function(player)
    local items = {player.Leaderstats.Points.Value}
    local key = "user-" .. player.userId

    datastore:SetAsync(key, items)
end)

In a localscript:

local Player = game.Players.LocalPlayer
local AddPointGui = script.Parent.Parent.StarterGui.HandtoGui.Accept.Yes
AddPointGui.MouseButton1Click:connect(function()
Player:WaitForChild("leaderstats").Points.Value = Player:WaitForChild("leaderstats").Points.Value + 1
end)
Ad

Answer this question