Gives me an error on line 10. This leaderstats is also suppose to datasave.

API Services are indeed turned on btw

local DataStore = game:GetService("DataStoreService")
local DataStore = DataStore:GetDataStore("Fossils")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Fossils = Instance.new("StringValue")
	Fossils.Name = "Fossils"
	Fossils.Value = DataStore:GetAsync(Player.UserId).Fossils or 0 -- Error: ServerScriptService.leaderstats:10: attempt to index nil with 'Fossils'
	Fossils.Parent = Leaderstats
	
	local Arti = Instance.new("StringValue")
	Arti.Name = "Artifacts"
	Arti.Value = DataStore:GetAsync(Player.UserId).Artifacts or 0 
	Arti.Parent = Leaderstats
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Fossils"] = Player.leaderstats.Fossils.Value;
		["Artifacts"] = Player.leaderstats.Artifacts.Value;
	})
end)

3 comments

0

If you never have the datastore setup for that key first then it will return nil. And nil.Fossils would error because Fossils is not a child of nil.

0

You explained my problem but I still do not know how to fix the problem.

0

Nvm I re-wrote it and it works.