Why isn't this datastore saving the player's 'Credits' when they 'Credit' amount changes or when the player leaves the game?

These are the only errors I get when it comes to the datastore (This is from an in game output): http://share.pho.to/AENyv

Code:

--Initial Section--

local DataStore = game:GetService("DataStoreService")
local curr = DataStore:GetDataStore("TrustCurrencySecondDraft") 
print("step1")

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "statStorage"
	print("step2")
	local Credits = Instance.new("IntValue",leader)
	Credits.Name = "Credits"
	print("step3")
	Credits.Value = curr:GetAsync(player.userId) or 100
	print("step4")	
	wait(.2)
	print("Initial Load Complete")
	
	
	Credits.Changed:connect(function()
		print("Saving Data")
		curr:SetAsync(player.userId,Credits.Value)
		print("Saved "..player.userId.."'s Credit Balance of "..Credits.Value)
	end)
end)


game.Players.PlayerRemoving:connect(function(player)
		curr:UpdateAsync(player.userId,player.statStorage.Credits.Value)
	print("Saved "..player.userId.."'s Credit Balance of "..player.StatStorage.Credits.Value)
end)

--End Initial Section--

--Daily Reward--

local DataStore = game:GetService('DataStoreService'):GetDataStore('DailyRewardsDraft')
local TimePeriod = 86400
local dailyIncrease = script.DailyReward.Value
print("Found Reward Store")
 
game.Players.PlayerAdded:connect(function(plr)
	print("Began Daily")
	local currenttime = os.time()
	local RecordedPreviousTime = DataStore:GetAsync(plr.userId)
	if RecordedPreviousTime then
		if type(RecordedPreviousTime) == 'number' then
		local timeelapsed = currenttime - RecordedPreviousTime
		print(timeelapsed)
			if timeelapsed >= TimePeriod then
			DataStore:SetAsync(plr.userId, os.time())
			plr.statStorage.Credits.Value = plr.statStorage.Credits.Value + dailyIncrease
		print("Another Daily Reward Given")
		--Gui Begin--
		local notice = script.DailyRewardGui:Clone()
		notice.Parent = plr
		--Gui End--
	else
		DataStore:SetAsync(plr.userId, os.time())
		plr.statStorage.Credits.Value = plr.statStorage.Credits.Value + dailyIncrease
		print("First Daily Reward Given")
		--Gui Begin--
		local notice = script.DailyRewardGui:Clone()
		notice.Parent = plr
		--Gui End--
		end
		end
	end
	end)
				
--End Reward Section--

--Game Closing Section--

game.OnClose = function()
	print("Closing")
wait(15) 
	print("Closed")
	wait(1)
end

--End Game Closing Section--

1 comment

0

Side Note: I have the Gui for 'notice' Visible already and it has a button to close it, but it won't display..