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)