How would I get specific datastore data?

Okay so the code is:
When you play the game you can get a value assigned to you found in your data store:
image
Now the 2 in use are jailReason and jailTime
Simple as, some other scripts change the values of those as you play.

The issue:
I haven’t got the faintest clue how to obtain that data when they rejoin the game, updating it is the easy part just re-putting it there is the hard part. (LINE 24 + 25)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)  -- Runs when players join
	local datastore = Instance.new("Folder")
	datastore.Name = "DataHost"
	datastore.Parent = player
	
	local jailFolder = Instance.new("Folder")
	jailFolder.Name = "jailHost"
	jailFolder.Parent = player.DataHost
	
	local ttime = Instance.new("IntValue")
	ttime.Name = "jailTime"
	ttime.Parent = jailFolder
	
	local reason = Instance.new("StringValue")
	reason.Name = "jailReason"
	reason.Parent = jailFolder
	--local jailreas = player.DataHost.jailHost.jailReason.Value
	--local jailtime= player.DataHost.jailHost.jailTime.Value
	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	if data then
		ttime.Value = data
		reason.Value = data
	else
		ttime.Value = 0 
		reason.Value = "N/A"
	end
end



local function onPlayerExit(player)  --Runs when players exit
	local jailreas = player.DataHost.jailHost.jailReason.Value
	local jailtime= player.DataHost.jailHost.jailTime.Value
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, jailreas) --Saves player data
		playerData:SetAsync(playerUserId, jailtime)
		warn(jailreas..jailtime)
	end)
	if not success then
		warn('Failed to save data.')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Line 24 sets the time value back to the value it was when you left
but obviously line 25 also sets the reason value to the time value (as that is what the variable data is as it would appear)
It’s late at night and I am unsure of whats going on in my own code, some guidance would be nice if anyone realises what’s wrong; it would be much appreciated.

so u wanna to save multiple data?

No i’m trying to get the already saved data

local function onPlayerExit(player)  --Runs when players exit
	local jailreas = player.DataHost.jailHost.jailReason.Value
	local jailtime= player.DataHost.jailHost.jailTime.Value
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, jailreas) --Saves player data
		playerData:SetAsync(playerUserId, jailtime)
		warn(jailreas..jailtime)
	end)
	if not success then
		warn('Failed to save data.')
	end
end

and put it here when they rejoin:
image
which is this line of code:

	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	if data then
		ttime.Value = data
		reason.Value = data
	else
		ttime.Value = 0 
		reason.Value = "N/A"
	end
end

but that line of code changes both values to the “timeValue”

Maybe this vid can help u, if it is please mark this as solution

1 Like

The video helped a lot for sure, there is however an error? Maybe this can help you understand how to fix it?
image

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)  -- Runs when players join
	local datastore = Instance.new("Folder")
	datastore.Name = "DataHost"
	datastore.Parent = player
	
	local jailFolder = Instance.new("Folder")
	jailFolder.Name = "jailHost"
	jailFolder.Parent = player.DataHost
	
	local ttime = Instance.new("IntValue")
	ttime.Name = "jailTime"
	ttime.Parent = jailFolder
	
	local reason = Instance.new("StringValue")
	reason.Name = "jailReason"
	reason.Parent = jailFolder
	--local jailreas = player.DataHost.jailHost.jailReason.Value
	--local jailtime= player.DataHost.jailHost.jailTime.Value
	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored
	if data then
		ttime.Value = data['jailTime']
		reason.Value = data['jailReason']
	else
		ttime.Value = 0
		reason.Value = "N/A"
	end
	local function create_table(player)
		local playerstats = {}
		for _, stat in pairs(player.DataHost.jailHost:GetChildren()) do
			playerstats[stat.Name] = stat.Value
		end
		return playerstats
	end
end



local function onPlayerExit(player)  --Runs when players exit
	local jailreas = player.DataHost.jailHost.jailReason.Value
	local jailtime= player.DataHost.jailHost.jailTime.Value
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, jailreas) --Saves player data
		playerData:SetAsync(playerUserId, jailtime)
		warn(jailreas..jailtime)
	end)
	if not success then
		warn('Failed to save data.')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

the issue is whe u try to save data

1 Like

check the data save part of the vid

1 Like

Yeah after rechecking the script, I realised I messed up, I changed the code to the following:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)  -- Runs when players join
	local datastore = Instance.new("Folder")
	datastore.Name = "DataHost"
	datastore.Parent = player
	
	local jailFolder = Instance.new("Folder")
	jailFolder.Name = "jailHost"
	jailFolder.Parent = player.DataHost
	
	local ttime = Instance.new("IntValue")
	ttime.Name = "jailTime"
	ttime.Parent = jailFolder
	
	local reason = Instance.new("StringValue")
	reason.Name = "jailReason"
	reason.Parent = jailFolder
	--local jailreas = player.DataHost.jailHost.jailReason.Value
	--local jailtime= player.DataHost.jailHost.jailTime.Value
	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored
	if data then
		ttime.Value = data['jailTime']
		reason.Value = data['jailReason']
	else
		ttime.Value = 0
		reason.Value = "N/A"
	end
end
local function create_table(player)
	local playerstats = {}
	for _, stat in pairs(player.DataHost.jailHost:GetChildren()) do
		playerstats[stat.Name] = stat.Value
	end
	return playerstats
end

local function onPlayerExit(player)  --Runs when players exit
	local playerstats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = 'Player_'..player.UserId
		playerData:SetAsync(playerUserId,playerstats)
	end)
	if not success then
		warn('Failed to save data.')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

And now it works, thank you for your help.