DataStore2 attempt to index nil with 'Coins'

there are a error massage in the output when i play the game.
ServerScriptService.DataMain:56: attempt to index nil with ‘Coins’
here is the script:

local DataStore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "MainKey"

DataStore2.Combine(MainKey, "Stat", "Achievements", "DailyReward")

--DataTable
local function SetDataTable()
	local UserData = {
		Stat = {
			["Coins"] = 0,
			["Ore"] = {},
		},
		Achievements = {
			["JoinedTheGame"] = false,
		},
		DailyReward = {
			["MainWorldCool"] = 0,
		},
	}
	return UserData
end

--Main
game.Players.PlayerAdded:Connect(function(plr)
	local UserData = DataStore2(MainKey,plr):Get(SetDataTable())
	
	--Folder
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local achfolder = Instance.new("Folder")
	achfolder.Name = "AchievementsFolder"
	local daifolder = Instance.new("Folder")
	daifolder.Name = "DailyRewardFolder"
	
	--Value
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	local Ores = Instance.new("IntValue")
	Ores.Name = "Ores"
	
	--Achievement
	local JoinedTheGame = Instance.new("BoolValue")
	JoinedTheGame.Name = "JoinedTheGame"
	
	--DailyReward
	local MainWorldCool = Instance.new("IntValue")
	MainWorldCool.Name = "MainWorldCool"
	
	local StatData = DataStore2("Stat", plr)
	local AchievementsData = DataStore2("Achievements", plr)
	local DailyRewardData = DataStore2("DailyReward", plr)
	
	wait(2)
	
	local function UpdateStats(UpdatedValue)
		Coins.Value = StatData:Get(UpdatedValue).Coins
		Ores.Value = StatData:Get(UpdatedValue).Ores	
	end
	
	local function UpdateAch(UpdatedValue)
		JoinedTheGame.Value = AchievementsData:Get(UpdatedValue).JoinedTheGame
	end
	
	local function UpdateDai(UpdatedValue)
		MainWorldCool.Value = DailyRewardData:Get(UpdatedValue).MainWorldCool
	end
	
	UpdateStats(UserData.Stat)
	UpdateAch(UserData.Achievements)
	UpdateDai(UserData.DailyReward)
	
	StatData.OnUpdate(UpdateStats)
	AchievementsData.OnUpdate(UpdateAch)
	DailyRewardData.OnUpdate(UpdateDai)
	
	
	leaderstats.Parent = plr
	achfolder.Parent = plr
	Coins.Parent = leaderstats
	Ores.Parent = leaderstats
	JoinedTheGame.Parent = achfolder
	MainWorldCool.Parent = daifolder
	
	--Give Stats
	
	--Coins Gui
	Coins.Value.Changed:Connect(function()
		local player = game.Players.LocalPlayer
		player.PlayerGui.Cash.Text.TextLabel.Text = Coins.Value
	end)
	
	--DailyRewardCool
	while MainWorldCool.Value > 0 do
		MainWorldCool.Value = MainWorldCool.Value -1 
		task.wait(1)
	end
	  --DailyReward
	local player = game.Players.LocalPlayer
	local UIS = game:GetService("UserInputService")
	local claimButton = player.PlayerGui.DailyRewardGui.Frame.button
	local waitHour = 24*60*60
	claimButton.MouseButton1Click:Connect(function()
		if MainWorldCool.Value == 0 then
			Coins.Value = Coins.Value + 200
			MainWorldCool.Value = waitHour	
		else return
		end
	end)
	
	
	--AutoSave
	while true do
		wait(200)
		local KeyData = DataStore2(MainKey)
		KeyData.Save()
	end
end)
1 Like

Your datastore code must to be in a script in the server script service and your gui code must to be in a local script in the starter gui or player scripts.

Also your datastore script should contain only datastore code, and not something else.

Don’t be afraid to create differents scripts for each things, it make your game more organized and you will find you’re code more faster and easier than writing everything in only one script and scrolling again and again to find what do you search.

local claimButton = player.PlayerGui.DailyRewardGui.Frame.button

You can’t do this, the script have 50% chance to don’t find the PlayerGui and what is inside it.
You need to use WaitForChild to be sure the script will find what do you search.

New

local Gui = Player:WaitForChild("PlayerGui" ,5)
if Gui ~= nil then
    local Daily = Gui:WaitForChild("DailyRewardGui" ,5)
    if Daily ~= nil then
        Daily.Frame.button.... code
    end
end

Yes this is annoying but this is necessary to make your code work properly

2 Likes

I see you are using instances for a table that works with cache, your code is trying to assign a value to an instance doing something that generates an error and is totally unnecessary, I would recommend that you just use the module’s cache and remove the instances for your script work correctly.

Another thing I ended up seeing is that you are using a local system on the server and this is not correct, it would be better if you worked with a RemoteFunction to receive the return of your DataStore data by the client and thus run the gui system on the client itself .

2 Likes

Use the code below to make your script work correctly.

--// Services
local plrs = game:GetService('Players')
local sss = game:GetService('ServerScriptService')

--// Requires
local ds2 = require(sss.DataStore2)

--// DataStore2
local mainkey = 'mainkey'

ds2.Combine(mainkey, 'stat', 'achievements', 'dailyrewards')

function setdatatable()
	return {
		stat = {
			Coins = 0,
			Ore = {},
		},
		achievements = {
			JoinedTheGame = false,
		},
		dailyrewards = {
			MainWorldCool = 0,
		},
	}
end

--// Functions
function playerAdded(plr)
	ds2(mainkey, plr):Get(setdatatable())

	while task.wait(200) do
		ds2(mainkey).Save()
	end
end

--// Connect
plrs.PlayersAdded:Connect(playersAdded)
2 Likes