Issues with if conditionals?

I’m making a milestone system, and unfortunately my server script for some reason doesn’t pass a certain conditional.I want it to only update the milestone table if:
A) The floor value is bigger or equal to 20
B) The certain index within the mile stone system hasn’t been used before
C) The player index of the table is empty

However, this doesn’t seem to be working.

Help?

Conditional

				if fetchedData["Milestone_"..i].PlayerName == "" and Player.HighestFloor.Value > fetchedData["Milestone_"..i].Requirement and  fetchedData["Milestone_"..i].Locked == false then

Full milestone system

local function manageMilestone(Player)
	local fetchedData
	local success, errormessage = pcall(function()
		fetchedData = milestoneGDatastore:GetAsync("MilestoneData") --Fetch data from ds
	end)
	if success then --if fetch success
		if fetchedData ~= nil then --if data retrieved isnt nil
			for i = 200, 1200, 100 do --For loop from 200 to 1200
				if fetchedData["Milestone_"..i].PlayerName == "" and Player.HighestFloor.Value > fetchedData["Milestone_"..i].Requirement and  fetchedData["Milestone_"..i].Locked == false then
					print("Passed conditional")
					fetchedData["Milestone_"..i].PlayerName = Player.Name
					fetchedData["Milestone_"..i].Locked = true
					fetchedData["Milestone_"..i].ID = Player.UserId
					fetchedData["Milestone_"..i].Date = tostring(DateTime.now():FormatLocalTime("LL", "en-us"))
					local newTable = {}
					for index, value in pairs(fetchedData["Milestone_"..i]) do
						newTable[index] = value
					end
					local updateSuccess, updateResult = pcall(function()
						milestoneGDatastore:SetAsync("MilestoneData", newTable)
					end)
					game.ReplicatedStorage.updateMilestones:FireAllClients(newTable)
					if not updateSuccess then
						warn(updateResult)
					end
				else
					print("Did not meet conditions...")
				end
			end
		else
			print("Presetting milestones...")
			local defaultTable = {}
			for i = 200, 1200, 100 do
				defaultTable["Milestone_"..i] = {
					PlayerName = "",
					ID = 0,
					Requirement = i,
					Locked = false,
					Date = tostring(DateTime.now())
				}
			end
			local saveSuccess, newErrormessage = pcall(function()
				milestoneGDatastore:SetAsync("MilestoneData", defaultTable)
			end)
			if  not saveSuccess then
				warn(newErrormessage)
			end
		end
	else
		warn(errormessage)
	end
end

I’ll assume you did the HighestFloor and Locked checks correctly, your issue could be the first statement, fetchedData["Milestone_"..i].PlayerName == "", do you directly set PlayerName to "" anywhere to indicate it is empty? If not, I would change it so it checks if it is not equal to nil, ~= nil

Chnaged it up a bit, it is now erroring:

local function manageMilestone(Player)
	local fetchedData
	local success, errormessage = pcall(function()
		fetchedData = milestoneGDatastore:GetAsync("MilestoneData") --Fetch data from ds
	end)
	if success then --if fetch success
		if fetchedData ~= nil then --if data retrieved isnt nil
			for i = 200, 1200, 100 do --For loop from 200 to 1200
				if fetchedData["Milestone_"..i].PlayerName ~= nil and Player.HighestFloor.Value > fetchedData["Milestone_"..i].Requirement and  fetchedData["Milestone_"..i].Locked == false then
					print("Passed conditional")
					fetchedData["Milestone_"..i].PlayerName = Player.Name
					fetchedData["Milestone_"..i].Locked = true
					fetchedData["Milestone_"..i].ID = Player.UserId
					fetchedData["Milestone_"..i].Date = tostring(DateTime.now():FormatLocalTime("LL", "en-us"))
					local newTable = {}
					for index, value in pairs(fetchedData["Milestone_"..i]) do
						newTable[index] = value
					end
					local updateSuccess, updateResult = pcall(function()
						milestoneGDatastore:SetAsync("MilestoneData", newTable)
					end)
					game.ReplicatedStorage.updateMilestones:FireAllClients(newTable)
					if not updateSuccess then
						warn(updateResult)
					end
				else
					print("Did not meet conditions...")
				end
			end
		else
			print("Presetting milestones...")
			local defaultTable = {}
			for i = 200, 1200, 100 do
				defaultTable["Milestone_"..i] = {
					PlayerName = "",
					ID = 0,
					Requirement = i,
					Locked = false,
					Date = tostring(DateTime.now())
				}
			end
			local saveSuccess, newErrormessage = pcall(function()
				milestoneGDatastore:SetAsync("MilestoneData", defaultTable)
			end)
			if  not saveSuccess then
				warn(newErrormessage)
			end
		end
	else
		warn(errormessage)
	end
end

Error:

 14:59:26.433  ServerScriptService.ServerDatastoreHandler:15: attempt to index nil with 'PlayerName'  -  Server

It’s erroring because fetchedData["Milestone_"..i] is nil, add a check at the start to check if it exists first

2 Likes