In my game I have a few chests spread around the map that allow users to see how many people made a donation to the game, and allows them to also donate 10 robux too (bit cheesy way to make some extra R$ I know)
Anyway I was wondering if it’s possible to detect a change in the DataStore that would fire everytime it’s updated, instead of having a constant loop watching for it. This is my current code (some functions/variables may not be defined for space’s sake)
local DS = game:GetService("DataStoreService"):GetDataStore("2PMT")
local notif = game.ReplicatedStorage:WaitForChild("Remote"):WaitForChild("SystemMessage")
local Raised = DS:GetAsync(CurrencyType.."_Raised")
game:GetService("RunService").Heartbeat:Connect(function() --- here's the loop
wait(1)
for model,chest in pairs(script.Parent:GetChildren()) do
if chest:IsA("Model") then
chest.Screen.SurfaceGui.Raised.ROBUX.Text = CurrencyType.." "..Raised
chest.Screen.CD.MouseClick:connect(function(Player)
if Debounce == false then
Debounce = true
if Player.userId > 0 then
MS:PromptProductPurchase(Player,ProductID)
end
wait(2)
Debounce = false
end
end)
end
end
end)
MS.PromptProductPurchaseFinished:connect(function(UserId,Id,IsPurchased) --- updates the datastore
if IsPurchased and Id == ProductID then
DS:IncrementAsync(CurrencyType.."_Raised",ProductPrice)
local Player = Players:GetPlayerByUserId(UserId)
---DS:OnUpdate(CurrencyType.."_Raised",PrintOut)
notif:FireClient(Player,"Thank you for donating, "..Player.Name.. "!",Enum.FontSize.Size10,Enum.Font.SourceSansItalic,Color3.fromRGB(255, 255, 10))
play_Sound(Player,"Buy",2)
if Player:FindFirstChild("Donator") == false then
local dohn = Instance.new("Model")
dohn.Name = "Donator"
dohn.Parent = Player
end
end
end)