Hey. I’m trying to use data store 2 on my game but i have a problem. I have a dedicated script on all npcs as a child. I’m rewarding players depending on the damage to the npc. Script is working fine and i can see correct updated value on leaderboard. Data store 2 looks like can’t catch it up with increment method. When i quit and rejoin my gold is less then it should be. Here is my script. Should i change the way i do this or there is another problem?
local npc = script.Parent
local hum = npc:WaitForChild("Humanoid")
local npcInfo = require(game:GetService("ReplicatedStorage"):WaitForChild("NpcInfo"))
local goldReward = npcInfo[npc.Name].GoldReward
--Data Sore 2
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine("DATA", "temp_gold")
--Create variables
local statusFolder = Instance.new("Folder")
statusFolder.Name = "Attackers"
statusFolder.Parent = npc
local debounce = false
hum.Died:Connect(function()
if debounce then return end
debounce = true
local attackers = statusFolder:GetChildren()
for _, attacker in pairs(attackers) do
local gold = game.Players:WaitForChild(attacker.Name):WaitForChild("leaderstats").Gold
if gold == nil then return end
local maxHealth = hum.MaxHealth
if attacker.Value > maxHealth then attacker.Value = maxHealth end
--Add actual gold to leaderboard
gold.Value = gold.Value + ((attacker.Value/maxHealth) * goldReward)
--Save the gold
local goldStore = DataStore2("temp_gold", player)
goldStore:Increment((attacker.Value/maxHealth) * goldReward)
end
end)
