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

Datastore isn't saving and "hearts" value isn't showing?

Can you tell me why this isn't working?


local DataStoreService = game:GetService("DataStoreService")
local store = DataStoreService:GetDataStore("PLSHELP")





local function leaderstat(plr)
	
	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"
	leader.Parent = plr
	
	local lettrboyos = Instance.new("IntValue")
	lettrboyos.Name = "Letters"
	lettrboyos.Parent = plr.leaderstats
	lettrboyos.Value = DataStoreService:GetAsync(plr.UserID) or 0
	
	DataStoreService:SetAsync(plr.UserID,lettrboyos.Value)
	
		lettrboyos.Value.Changed:Connect(function()
			
			DataStoreService:SetAsync(plr.UserID,lettrboyos.Value)
			
	end)
	
	local heartstaken = Instance.new("IntValue")
	heartstaken.Name = "Hearts"
	heartstaken.Parent = plr.leaderstats
	heartstaken.Value = DataStoreService:GetAsync(plr.UserID) or 0
	
	DataStoreService:SetAsync(plr.UserID,heartstaken.Value)
	
		heartstaken.Value.Changed:Connect(function()
			
			DataStoreService:SetAsync(plr.UserID,heartstaken.Value)
			
	end)
	
	game.Players.PlayerRemoving:Connect(function()
		
		DataStoreService:SetAsync(plr.UserID,lettrboyos.Value)
		DataStoreService:SetAsync(plr.UserID,heartstaken.Value)

	end)
end

game.Players.PlayerAdded:Connect(leaderstat)


2 comments

0

It doesn't work because you are overriding the data in the database. You need either 2 separate databases or another key to separate them.

0

how would I go about doing this? Sorry am new to lua

1 answer

If you want create a real DataBase, check this script have make for you.

local PlayerService = game:GetService('Players')
local RunService = game:GetService('RunService')
local HttpService = game:GetService('HttpService')
local DataStoreService = game:GetService('DataStoreService')

local Store = DataStoreService:GetDataStore('PLSHELP')

local SessionData = {} -- Here you can stock the datas session of your current player

PlayerService.PlayerAdded:Connect(function(plr) -- You can put the function directly in the connection.
	local cid = plr.UserId
	
	SessionData[cid] = { -- Create a session
		data = {
			lettrboyos = 0 -- Set the value per default to 0
		},
		update_lst = Instance.new('BindableEvent'), -- To update your leaderstats
		save_function = nil -- To save your data
	}
	
	if not RunService:IsStudio() then -- Check if you're run on Studio because DataStore doesn't working on Studio.
		local get_store = Store:GetAsync(cid) -- get store
		if get_store ~= nil and type(get_store) == 'string' then
			pcall(function()
				local decode = HttpService:JSONDecode(get_store)
				if type(decode) == 'table' then
					SessionData[cid].data = decode -- You have retry your data congrats!
				end
			end)
		end
	end
	
	local lst = Instance.new('Folder', plr)
	lst.Name = 'leaderstats'
	
	local lttbs = Instance.new('IntValue', lst)
	lttbs.Name = 'letter'
	
	SessionData[cid].update_lst.Event:Connect(function()
		lttbs.Value = SessionData[cid].data.lettrboyos -- set your value
	end)
	
	SessionData[cid].save_function = function()
		if not RunService:IsStudio() then
			pcall(function()
				Store:SetAsync(cid, HttpService:JSONEncode(SessionData[cid].data)) -- Here we use JSONEncode to tranforme table to string, because DataStore can't save tables.
			end)
		end
	end
	
	SessionData[cid].data.lettrboyos = 10
	SessionData[cid].update_lst:Fire()
	
	SessionData[cid].save_function() -- Saving
	
end)

PlayerService.PlayerRemoving:Connect(function(plr)
	local cid = plr.UserId
	
	SessionData[cid].save_function() -- Saving
end)

spawn(function() -- Create a new coroutine
	while wait(60) do -- Save players data all 60 secondes ago.
		for i, v in next, SessionData do
			if type(v) == 'table' and v['save_function'] ~= nil then -- verification step
				v.save_function()
			end
		end
	end
end)

Now if you want share SessionData with all script you can use _G to create a Global Varaible than all script can use.

local SessionData = {} -- Here you can stock the datas session of your current player
_G.ProfileData = SessionData

You can find this variable like that with other script

local ProfileData = (function()
	repeat wait()
		if _G.ProfileData ~= nil then
			return _G.ProfileData
		end
	until _G.ProfileData -- waiting _G.ProfileData was be created
end)()

To learn more about this script

HttpService RunService JSONEncode PlayerStatManager

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