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

What have I done wrong with this DataStore script?

Hello, This script is supposed to just save and load one value in leaderstats. I've tried so many methods to get this to work, moving the script around, I turned on API. It just refuses to save the value and I just don't know whats wrong anymore. Please can anybody experienced enough with DataStore tell me why this won't function?

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("Currency")

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

	local stats = Instance.new("Folder",player)
	stats.Name = "leaderstats"
	local Schmeckles = Instance.new("IntValue",stats)
	Schmeckles.Name = "Schmeckles"
	
	local key = "player-"..player.userId
	Schmeckles.Value = ds1.GetAsync(key) or 0
	ds1:SetAsync(key, Schmeckles.Value)
	Schmeckles.Changed:connect(function()
		ds1:SetAsync(key, Schmeckles.Value)
		
	end)
end)

 game.Players.PlayerRemoving:connect(function(player)
	local key = "player-"..player.userId
	ds1:SetAsync(key, player.leaderstats.Schmeckles.Value)
	
end)

game.OnClose = function()
	wait(10)
end

2 comments

0

Remove lines 13 - 17, as well as 26 - 28. There is no need for them. Everything else looks fine. Make sure this script is in Workspace or ServerScriptStorage.

0

They're used to update the value whenever it's changed and to allow the datastore to update before the game closes respectively. This script is in ServerScriptStorage with API turned on. I still don't see why the script isn't working

1 answer

Here is a Script and a ModuleScript. There are two reason that I am using a ModuleScript: 1st it allows you to have shared code between several scripts (code once use many times), 2nd I had already written it.

So here it is:

Structure of game.ServerScriptService

--game
	--ServerScriptService
		--DS			--ModuleScript
		--StatControl	--Script

Module script in game.ServerScriptService

--Created by shadownetwork
--Created 02/21/2017
--Last Edited 02/21/2017

--DataStore API

--Accepts
	--Store as string
	--Key as string
	--Value as number, boolean, string, or Lua table
	--UpdateData only takes Value as number
	
--Examples
--	require(workspace.DS):SetData("PlayerIDToName", 1, "Roblox")
--	PlayerName = require(_G.M.DS):GetData("PlayerIDToName", 1)
--	require(workspace.DS):ForceSetData("PlayerIDToName", 1, "WrongName")
--	PlayerName = require(_G.M.DS):GetData("PlayerIDToName", 1)
--	require(workspace.DS):UpdateData("Stats", "Visits", +1)

local DataStoreService = game:GetService('DataStoreService')

T = {DataStoreRetries = 5}

function T:GetData(Store, Key) -- Checks to see if data exist
	Data = nil
	if Store and Key then
		--print("Getting Data")
		local tries = 0
		repeat
			tries = tries + 1
			success, message = pcall(function() 
				DataStore = DataStoreService:GetDataStore(Store)
				Data = DataStore:GetAsync(Key)
			end)
			if not success then
				wait(1)
			end
		until tries == T.DataStoreRetries or success
		if not success then
			print("")
			print("require(_G.M.DS):GetData, Error Data not loaded from DataStore")
			print("Current values: Store = "..Store..", Key = "..Key)
			error(message)
		end
	end
	return Data
end

function T:SetData(Store, Key, Value) -- Sets data if it doesn't exist
	if Store and Key and Value then
		--print("Setting Data")
		local tries = 0
		repeat
			tries = tries + 1
			success, message = pcall(function() 
				DataStore = DataStoreService:GetDataStore(Store)
				if not T:GetData(Key) then
					DataStore:SetAsync(Key, Value)
				end
			end)
			if not success then
				wait(1)
			end
		until tries == T.DataStoreRetries or success
		if not success then
			print("")
			print("require(_G.M.DS):SetData, Error Data not saved to DataStore")
			print("Current values: Store = "..Store..", Key = "..Key..", Value = "..Value)
			error(message)
		end
	end
end

function T:ForceSetData(Store, Key, Value) -- Overwrites old data
	if Store and Key and Value then
		--print("Overwriting Data")
		local tries = 0
		repeat
			tries = tries + 1
			success, message = pcall(function() 
				DataStore = DataStoreService:GetDataStore(Store)
				DataStore:SetAsync(Key, Value)
			end)
			if not success then
				wait(1)
			end
		until tries == T.DataStoreRetries or success
		if not success then
			print("")
			print("require(_G.M.DS):ForceSetData, Error Data not saved to DataStore")
			print("Current values: Store = "..Store..", Key = "..Key..", Value = "..Value)
			error(message)
		end
	end
end

function T:UpdateData(Store, Key, Value) -- Updates number and int values
	if Store and Key and Value then
		--print("Updating Data")
		local tries = 0
		repeat
			tries = tries + 1
			success, message = pcall(function() 
				DataStore = DataStoreService:GetDataStore(Store)
				DataStore:UpdateAsync(Key, function(OldValue)
					OldValue = OldValue or 0
					local NewValue = OldValue
					if type(Value) == "number" then
						NewValue = NewValue + Value
					end
					return NewValue
				end)
			end)
			if not success then
				wait(1)
			end
		until tries == T.DataStoreRetries or success
		if not success then
			print("")
			print("require(_G.M.DS):UpdateData, Error Data not saved to DataStore")
			print("Current values: Store = "..Store..", Key = "..Key..", Value = "..Value)
			error(message)
		end
	end
end

return T

Script in game.ServerScriptService

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("Currency")
ID = "player"

game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("Folder",player)
    stats.Name = "leaderstats"
    local Schmeckles = Instance.new("IntValue",stats)
    Schmeckles.Name = "Schmeckles"

    local key = ID..player.userId
    Schmeckles.Value = require(game.ServerScriptService.DS):GetData("Currency", key) or 0
end)

 game.Players.PlayerRemoving:connect(function(player)
    local key = ID..player.userId
	require(game.ServerScriptService.DS):ForceSetData("Currency", key, player.leaderstats.Schmeckles.Value)
end)

while wait(60) do
	P = game:GetService("Players"):GetChildren()
	for i=1, #P do
		if P[i]:FindFirstChild("leaderstats") then
			if P[i].leaderstats:FindFirstChild("Schmeckles") then
				local key = ID..P[i].userId
				require(game.ServerScriptService.DS):ForceSetData("Currency", key, P[i].leaderstats.Schmeckles.Value)
			end
		end
	end
end

game.OnClose = function()
    wait(10)
end

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