Help with script (Interfering with leaderstats saving to datastore)


local dailyRewardDS = dss:GetDataStore("DailyRewards")


local rewardsForStreak = 
{
		[1] = 5000,
		[2] = 6000,
		[3] = 7000,
		[4] = 8000,
		[5] = 9000,
		[6] = 10000,
		[7] = 100000,
}


game.Players.PlayerAdded:Connect(function(plr)
	
	local success, dailyRewardInfo = pcall(function()
		return dailyRewardDS:GetAsync(plr.UserId .. "-DailyRewards")
	end)
	if type(dailyRewardInfo) ~= "table" then dailyRewardInfo = {nil, nil, nil} end
	
	local leaderstats = plr:FindFirstChild("leaderstats")
	if not leaderstats then
		leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = plr
	end

	local credits = leaderstats:FindFirstChild("Credits")
	if not credits then
		credits = Instance.new("IntValue")
		credits.Name = "Credits"
		credits.Parent = leaderstats
		
	end
	
	credits.Value = dailyRewardInfo[1] or 0
	
	local lastOnline = dailyRewardInfo[2]
	local currentTime = os.time()
	
	local timeDifference
	
	if lastOnline then	
		timeDifference = currentTime - lastOnline
	end
	
	if not timeDifference or timeDifference >= 24*60*60 then
		
		local streak = dailyRewardInfo[3] or 1
		local reward = rewardsForStreak[streak]
		
		local dailyRewardGui = plr.PlayerGui:WaitForChild("DailyRewardGui")
		local mainGui = dailyRewardGui:WaitForChild("MainGui")
		local claimBtn = mainGui:WaitForChild("ClaimButton")
		local rewardLabel = mainGui:WaitForChild("RewardLabel")
		
		rewardLabel.Text = reward
		
		dailyRewardGui.Enabled = true
		
		claimBtn.MouseButton1Click:Connect(function()
			
			credits.Value = credits.Value + reward
			
			dailyRewardGui.Enabled = false
			
			local streak = streak + 1
			if streak > 7 then streak = 1 end
			
			local success, errormsg = pcall(function()
				
				dailyRewardDS:SetAsync(plr.UserId .. "-DailyRewards", {credits.Value, os.time(), streak})
			end)
		end)
		
	elseif timeDifference then
		
		wait((24*60*60) - timeDifference)
		
		if game.Players:FindFirstChild(plr) then
			
			local streak = dailyRewardInfo[3] or 1
			local reward = rewardsForStreak[streak]
			
			local dailyRewardGui = plr.PlayerGui:WaitForChild("DailyRewardGui")
			local mainGui = dailyRewardGui:WaitForChild("MainGui")
			local claimBtn = mainGui:WaitForChild("ClaimButton")
			local rewardLabel = mainGui:WaitForChild("RewardLabel")
			
			rewardLabel.Text = reward
			
			dailyRewardGui.Enabled = true
			
			claimBtn.MouseButton1Click:Connect(function()
				
				credits.Value = credits.Value + reward
				
				dailyRewardGui.Enabled = false
				
				local streak = streak + 1
				if streak > 7 then streak = 1 end
				
				pcall(function()
					
					dailyRewardDS:SetAsync(plr.UserId .. "-DailyRewards", {credits.Value, os.time(), streak})
				end)
			end)
		end
	end
end)```

Can you please provide additional information?

What are you trying to achieve?
What currently happens?
What have you already tried?

When this script is in it seems to interfere with the datastore. if a player earns money and leaves the game and enters the game again it doesn’t save the progress before. I took this script out and tested it and this script was the source. I just need to see what was wrong in this script.

Found the problem. Just had to rename the GetDataStore the same name as the datastorage line thats in the leaderstats script.

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