How to save data table in game in right way

How can I get data from the data store and store it in a table in the game so that I can query the data from the table later?

The problem is that I must use LoadData() before CheckData(), is there a way to just use CheckData() without using LoadData()?

local PlayersData = {}

function LoadData(Player,Slot)

	PlayersData[Player.Name] = {
		["Something"] = false
	}

	print(PlayersData) -- this shows playername and "Something" inside
end

function CheckData(Player,Slot,...) 
	local path = {...}

	print(PlayersData) -- this shows nothing in table
end
1 Like

i suggest using ProfileService, its very good at this and helps prevent data loss

I think Suphi´s Datastore is a bit better to use: (2) Suphi’s DataStore Module Basics - Roblox Scripting Tutorial - YouTube

Wanna know the difference? Check: (2) DataStore2 vs ProfileService vs Suphi’s DataStore Module - YouTube

There is a lack of data that I can draw from the script you gave? How are you calling the functions? A function can’t run if it is not called.

interesting, does this still use tables to store everything and save to datastore only when the player leaves? i may switch to this

I need to call the LoadData() function once (when player joins the game) to save the data in the game until the player leaves, so that every time I use CheckData() it will give me the latest saved data.

You can use a conditional statement to check if the player index exists in the PlayerData:

function CheckData(Player,Slot,...) 
	local path = {...}
        if PlayersData[Player.Name] then
        print("Data found")
        --Add code that handles data if there is data loaded
        else
        print("No Data found")
        --Add code that handles data if there is none loaded
	print(PlayersData) -- this shows nothing in table
end

Edit: Apologies for poor format, the tab button wasn’t working for while I was writing the post

problem was that i used module, not the server script, so module data saves only in one script where it was called

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.