How would I Prevent Errors here?

Hi,

So I am wondering how would I prevent errors with this code I made for fun?
I have tried checking if the Value existed but it didn’t seem to work:

function module.Test(Options: {})
	local DataStoreOptions = Instance.new("DataStoreOptions") -- DataStoreOptions
	
	for i,v in Options do
		if DataStoreOptions[i] then -- if DataStoreOptions.Value (error line)
			DataStoreOptions[i] = v -- Assigns Value
		else -- if no
			warn("cannot find Property '"..v.."', Skipping Unknown Property.")
			continue
		end
	end
	
	return DataStoreOptions -- returns Options
end
Module.Test({
	AllScopes = true; -- Works
	New = true; -- Test that didnt work
})

I probably wont really use this as it is unnecessary to do so.

Anyway, I found a Solution, it turns out I had to use a pcall()

function module.Test(Options: {})
	local DataStoreOptions = Instance.new("DataStoreOptions")
	
	for i,v in Options do
		local Found = pcall(function()
			local property = DataStoreOptions[i]
		end)
		
		if Found then
			DataStoreOptions[i] = v
		else
			warn("cannot find Property '"..i.."', Skipping Unknown Property.")
			continue
		end
	end
	
	return DataStoreOptions
end
1 Like

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