Datastore returns nil, and orange lines

I am trying to make a datastore for time. It is not working, however, as it returns nil on one of it’s lines. The 27th. This is my code.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStoreService = game:GetService("DataStoreService")
local longestLifeStore = DataStoreService:GetDataStore("PlayerLongestLife")

local AddTime = ReplicatedStorage.AddTime
local Died = ReplicatedStorage.Died
--Creates stats
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local timeAlive = Instance.new("IntValue", leaderstats)
	timeAlive.Name = "Time Alive"	
	
	local longestTimeAlive = Instance.new("IntValue", leaderstats)
	longestTimeAlive.Name = "Longest Time Alive"
	
	local data
		local success, errormessage pcall(function()
			data = longestLifeStore:GetAsync(player.UserId.."--LongestLife")
	end)
	if success then
		longestTimeAlive.Value = data
	else
		warn(errormessage)
	end
end)
Players.PlayerRemoving:Connect(function(player)
	local success, errormessage pcall(function()
		longestLifeStore:SetAsync(player.UserId.."--LongestLife",player.leaderstats[longestTimeAlive])
	end)
	if success then
		
	else
		warn(errormessage)
	end
end)
--Add time to players
AddTime.OnServerEvent:Connect(function(player, ptime, pmtime)
	ptime.Value += 1
	if ptime.Value >= pmtime.Value then
		pmtime.Value = ptime.Value
	end
end)
--Player dies
Died.OnServerEvent:Connect(function(player, ptime)
	for i = 1, 100 do
		ptime.Value = 0
		wait(.01)
	end
end)

Also, I don’t know why this is happening, but on line 24, “success” is underlined, as well as line 27, line 34, and line 37. Please help.

I didnt check the logic of your script, only the mispellings. You were missing the equal symbol before the pcall() 2 times, and you are trying to access a variable that doesnt exist inside the scope, I changed from:
longestLifeStore:SetAsync(player.UserId.."--LongestLife",player.leaderstats[longestTimeAlive])

to:

longestLifeStore:SetAsync(player.UserId.."--LongestLife", player.leaderstats:FindFirstChild("Longest Time Alive").Value )

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStoreService = game:GetService("DataStoreService")
local longestLifeStore = DataStoreService:GetDataStore("PlayerLongestLife")

local AddTime = ReplicatedStorage.AddTime
local Died = ReplicatedStorage.Died
--Creates stats
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local timeAlive = Instance.new("IntValue", leaderstats)
	timeAlive.Name = "Time Alive"	

	local longestTimeAlive = Instance.new("IntValue", leaderstats)
	longestTimeAlive.Name = "Longest Time Alive"

	local data
	local success, errormessage = pcall(function()
		data = longestLifeStore:GetAsync(player.UserId.."--LongestLife")
	end)
	if success then
		longestTimeAlive.Value = data
	else
		warn(errormessage)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		longestLifeStore:SetAsync(player.UserId.."--LongestLife", player.leaderstats:FindFirstChild("Longest Time Alive").Value )
	end)
	if success then

	else
		warn(errormessage)
	end
end)
--Add time to players
AddTime.OnServerEvent:Connect(function(player, ptime, pmtime)
	ptime.Value += 1
	if ptime.Value >= pmtime.Value then
		pmtime.Value = ptime.Value
	end
end)
--Player dies
Died.OnServerEvent:Connect(function(player, ptime)
	for i = 1, 100 do
		ptime.Value = 0
		wait(.01)
	end
end)

Please add the “=” on pcalls, otherwise, the function works, but way less, making the errors, successes adding a warning to the script analysis.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStoreService = game:GetService("DataStoreService")
local longestLifeStore = DataStoreService:GetDataStore("PlayerLongestLife")

local AddTime = ReplicatedStorage.AddTime
local Died = ReplicatedStorage.Died
--Creates stats
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local timeAlive = Instance.new("IntValue", leaderstats)
	timeAlive.Name = "Time Alive"	

	local longestTimeAlive = Instance.new("IntValue", leaderstats)
	longestTimeAlive.Name = "Longest Time Alive"

	local data
	local success, errormessage = pcall(function()
		data = longestLifeStore:GetAsync(player.UserId.."--LongestLife")
		if data == nil then
			warn("WHY IS IT NIL?!")
		end
	end)
	if data == nil then
		task.wait(0.8)
		print("Data could not be fetched, it's nil!")
	elseif data ~= nil then
		if success then
			longestTimeAlive.Value = data
		end
	end
end)
Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		longestLifeStore:SetAsync(player.UserId.."--LongestLife",player.leaderstats["Longest Time Alive"].Value)
	end)
	if success then
		--return here or put code here
	else
		warn(errormessage)
	end
end)
--Add time to players
AddTime.OnServerEvent:Connect(function(player, ptime, pmtime)
	ptime.Value += 1
	if ptime.Value >= pmtime.Value then
		pmtime.Value = ptime.Value
	end
end)
--Player dies
Died.OnServerEvent:Connect(function(player, ptime)
	for i = 1, 100 do
		ptime.Value = 0
		wait(.01)
	end
end)

Fetched: Reached.

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