How Would I Make This Coin System Using Datastore 2?

I am using Datastore 2 because I used it in my other game and it is amazing!
I am not the best scripter, but I know the basics and a little advanced stuff(for loops, tables, events)
Anyways, I am making a game called Strange World 3 and its basically about this:

  • Collecting Coins To Unlock New Areas
  • The Areas Are Going To Be Tycoons, Simulators, survival/ gun games, and obbies
  • You Collect Gems To Unlock Trails, Coin Collecting Areas In The Shop
  • You Level Up To Unlock Titles And Some Other Types Of Areas

This game came from my other game Strange World 2 but there was a lot of datastore problems and other things. The game didn’t really look appealing as well. When I switched to Datastore 2, There wasn’t a single incident of data loss!(Well except everyone had to start over at 0 with there data because I switched data stores, but that’s one of the main reasons why I am making a newer version)

This is my current script for collecting a coin worth 1. Every time you collect a coin, you collect one xp as well

local MarketPlaceService = game:GetService("MarketplaceService")
local Player = game:GetService("Players")

-----------These are for gamepasses-------

local twotimesID = 7158220 
local threetimesId = 7172665
local fivetimesId  = 7172700

------------------------------


script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = Player:GetPlayerFromCharacter(hit.Parent)

			--------------------------------------------------------------------
			local dS2 = require(game.ServerScriptService:WaitForChild"MainModule")
			local dM = 0
			local leaderstats = player:FindFirstChild("leaderstats")                              ----------------Leaderstat Vars.
			local coins = leaderstats.Coins
			local xp = leaderstats.XP
			local coinDT = dS2("Coins",player)
			local xpDT = dS2("XP",player)
			----------------------------------------------------------------------
			
			
			local function coinUp(UpValue)
				coins.Value = coinDT:Get(UpValue)
			end
			
			local function xpUp(UpXpValue)-- This Is For A Level Up System
				xp.Value = xpDT:Get(UpXpValue)
			end
			
			
			
			
			
			
			--------------------------------------------------------------------------
			
			
			
			
			
			
			
			
			
			 if MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), twotimesID) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), threetimesId)== nil and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), fivetimesId)== nil  then
			
			--owns 2x--
			coinUp(dM)
			coinDT:OnUpdate(coinUp)	
			local coinDT = dS2("Coins",player)
			coinDT:Increment(2,dM)	
			
		
			  elseif MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), twotimesID) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), threetimesId) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), fivetimesId)== nil then
				--owns 2x and 3x--
			
						coinUp(dM)
			coinDT:OnUpdate(coinUp)	
			local coinDT = dS2("Coins",player)
			coinDT:Increment(6,dM)
			
			
			 elseif MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), twotimesID) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), fivetimesId) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), threetimesId)== nil then
			
			--owns 2x and 5x--
			
			
						coinUp(dM)
			coinDT:OnUpdate(coinUp)	
			local coinDT = dS2("Coins",player)
			coinDT:Increment(10,dM)
			
			
              elseif MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), fivetimesId) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), threetimesId) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), twotimesID)== nil then
			--owns 3x and 5x--
						coinUp(dM)
			coinDT:OnUpdate(coinUp)	
			local coinDT = dS2("Coins",player)
			coinDT:Increment(15,dM)
			
			 elseif MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), threetimesId) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), twotimesID)== nil and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), fivetimesId)== nil then
			--owns 3x--
						coinUp(dM)
			coinDT:OnUpdate(coinUp)	
			local coinDT = dS2("Coins",player)
			coinDT:Increment(3,dM)
			
			
			 elseif MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), fivetimesId) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), threetimesId) == nil and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), twotimesID)== nil then
			--owns 5x--
						coinUp(dM)
			coinDT:OnUpdate(coinUp)	
			local coinDT = dS2("Coins",player)
			coinDT:Increment(5,dM)
			
			
			
			
			  elseif MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), twotimesID) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), threetimesId) and MarketPlaceService:UserOwnsGamePassAsync(Player:GetUserIdFromNameAsync(hit.Parent.Name), fivetimesId) then
			-- owns 2x 3x 5x---
			
						coinUp(dM)
			coinDT:OnUpdate(coinUp)	
			local coinDT = dS2("Coins",player)
			coinDT:Increment(30,dM)
			
			else
					coinUp(dM)
					coinDT:OnUpdate(coinUp)	
					local coinDT = dS2("Coins",player)
					coinDT:Increment(1,dM)
			
			
			end
			
			xpUp(dM)
			xpDT:OnUpdate(xpUp)	
			local xpDT = dS2("XP",player)
			xpDT:Increment(1,dM)	
			
		
			

			
			
			script.Sound:Play()
			wait(60)
			script.Parent.Transparency = 0
			db = true
			
		end
	end	
end)  

I am wondering if there is another way I could do this, that would be better than this. I also want to have a rebirth system in my game, and I have no idea how I would script that since I am only a inter. mediate scripter. Lemme know what your ideas are. Thx!

7 Likes

This thread would be better suited for #development-support:code-review which is dedicated to reviewing working code and providing improvement tips. Scripting Support is alright too but usually this category is used for debugging or fixing broken code.

2 Likes

oops, that was a mistake. sorry, all fixed