Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Adding Datastore Service?

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 comment

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.