I am using the Collection Service, datastore2, and gamepasses for the coins just to be clear
The coin is in the workspace, and I used the tag plugin to tag it. There is no script inside it.
In the Starter player scripts, I have the following script:
local collectS = game:GetService("CollectionService")
local coin1tagged = collectS:GetTagged("Coin1")
local playerService = game:GetService("Players")
for _, coin1tagged in pairs(coin1tagged) do
coin1tagged.Touched:Connect(function(hit)
local debounce = true
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if debounce == true then
debounce = false
coin1tagged.Transparency = 1
local player = playerService:GetPlayerFromCharacter(hit.Parent)
if player ~= nil then
game.ReplicatedStorage.CoinRemoteEvents.RemoteEvent:FireServer(player, 1)
coin1tagged.Parent = game.ReplicatedStorage.Storage
wait(60)
debounce = true
end
end
end
end)
end
In the ServerScriptService, I have the following script:
local coinsTimers = {} -- our new array that will track coin pick up times!
local regenTime = 10 -- set this to your regeneration timer in seconds
game:GetService("ReplicatedStorage").CoinRemoteEvents.RemoteEvent.OnServerEvent:Connect(function(player, coin) -- your awardCoin remote event (or whatever you refer to it as ;p).
local awardCoin = false -- if this is not met as true by the end of the checks, coin is not awarded.
if coinsTimers[player.Name] ~= nil then -- has the player recieved a coin? If not they won't have a value in the array yet.
local coinFound = false -- if this is not found in the array and set to true, the coin has not been picked up yet and thus will be added with a timer and awardCoin will be set to true.
local i = 0 -- track how many times the loop has ran in case we need to remove the old tick() value if the coin is eligible to be picked up.
for savedCoin, lastTime in pairs(coinsTimers[player.Name]) do -- loop through all the current saved data in the array. Unfortunately, we cannot directly refer to the coin since we also have to save the tick as well.
i = i + 1
if coin == savedCoin then -- if this is the coin we are looking for..
coinFound = true -- we found the coin!
if tick() - lastTime > regenTime then -- check if it has been long enough!
awardCoin = true -- this coin should be awarded!
table.remove(coinsTimers, i) -- delete the old time assigned to the coin.
table.insert(coinsTimers, {coin, tick()}) -- add the new time assigned to the coin.
end
break
end
end
if not coinFound then -- if coin wasn't found in the saves, award the coin!
table.insert(coinsTimers[player.Name], {coin, tick()})
awardCoin = true
end
else -- the player does not have data yet, lets add it!
awardCoin = true
coinsTimers[player.Name] = {}
table.insert(coinsTimers[player.Name], {coin, tick()})
end
if awardCoin then -- the checks proved the coin should be awarded, lets give them a coin!
-------------------------------------------------------------
--This is datastore2 and gamepasses
local MarketplaceService = game:GetService("MarketplaceService")
local playerService = game:GetService("Players")
local DataStore2 = require(game:GetService("ServerScriptService"):WaitForChild"MainModule")
local defaultAmount = 1
local twotimesID = 7158220 --2x
local threetimesID = 7172665 --3x
local fivetimesID = 7172700 --5x
local leaderstats = player.leaderstats
local coins = leaderstats.Coins
local xp = leaderstats.XP
local coinDataStore = DataStore2("Coins",player)
local expDataStore = DataStore2("XP",player)
local function coinUp(UpValue)
coins.Value = coinDataStore:Get(UpValue)
end
local function expUp(UpExpValue) -- This is for a level up system
xp.Value = expDataStore:Get(UpExpValue)
end
if MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),twotimesID) and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),threetimesID)== nil and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),fivetimesID)== nil then
coinDataStore:OnUpdate(coinUp) --owns ONLY 2x
coinDataStore:Increment(2 * defaultAmount,0)
elseif MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),twotimesID)== nil and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),threetimesID) and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),fivetimesID)== nil then
coinDataStore:OnUpdate(coinUp) --owns ONLY 3x
coinDataStore:Increment(3* defaultAmount,0)
elseif MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),twotimesID)== nil and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),threetimesID)== nil and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),fivetimesID) then
coinDataStore:OnUpdate(coinUp) --owns ONLY 5x
coinDataStore:Increment(5* defaultAmount,0)
elseif MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),twotimesID)and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),threetimesID) and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),fivetimesID)== nil then
coinDataStore:OnUpdate(coinUp) --owns ONLY 2x and 3x
coinDataStore:Increment(2*3* defaultAmount,0)
elseif MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),twotimesID)and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),threetimesID)== nil and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),fivetimesID) then
coinDataStore:OnUpdate(coinUp) --owns ONLY 2x and 5x
coinDataStore:Increment(2*5* defaultAmount,0)
elseif MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),twotimesID)== nil and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),threetimesID) and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),fivetimesID) then
coinDataStore:OnUpdate(coinUp) --owns ONLY 3x and 5x
coinDataStore:Increment(3*5* defaultAmount,0)
elseif MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),twotimesID)and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),threetimesID) and MarketplaceService:UserOwnsGamePassAsync(playerService:GetUserIdFromNameAsync(player.Name),fivetimesID) then
coinDataStore:OnUpdate(coinUp) --owns all coin gamepasses
coinDataStore:Increment(2*3*5* defaultAmount,0)
else
coinDataStore:OnUpdate(coinUp) --owns none----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- HE IS POOR XD
coinDataStore:Increment(defaultAmount,0)
end
end
end)
Please let me know if there are any mistakes.I did test this and it doesnât work, so let me know what I did wrong. Thanks!