I'm working on an rpg mob's drop system. I've opted for Datastore2. The data (xp, gold, levels) doesn't seem to be saving though. I always get this error on levelDataStore:Increment(Config.AwardedLvl): ServerScriptService.MainModule:253: attempt to perform arithmetic (add) on function and number.
Here's the stat award script:
if hit.Parent.Parent then --Defined via touched event
local Player = hit.Parent.Parent
if game.Players:GetPlayerFromCharacter(Player) then
local PlayerObj = game.Players:GetPlayerFromCharacter(Player)
print(PlayerObj)
local goldDataStore = Datastore2("gold", PlayerObj)
local expDataStore = Datastore2("exp", PlayerObj)
local levelDataStore = Datastore2("level", PlayerObj)
if Player:FindFirstChild("Humanoid") then
local Human = Player:FindFirstChild("Humanoid")
if Config.RestoreHealth == true then
Human.Health = Human.MaxHealth
end
goldDataStore:Increment(Config.AwardedGold)
expDataStore:Increment(Config.AwardedXP)
levelDataStore:Increment(Config.AwardedLvl)
print("Stats Awarded")
Here's the Datastore Handler:
-- Datastore2 Declarations
local Datastore2 = require(SSS.MainModule) --SSS is refers to ServerScriptService and Main Module is of Datastore2
local defaultGold = GameConfig.DefaultGold
local defaultExp = GameConfig.DefaultExp
local defaultLevel = GameConfig.DefaultLevel
Datastore2.Combine("Data", "gold", "exp", "level")
-- Create leaderstats
game.Players.PlayerAdded:Connect(function(plr)
local goldDataStore = Datastore2("gold", plr)
local expDataStore = Datastore2("exp", plr)
local levelDataStore = Datastore2("level", plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Gold = Instance.new("IntValue", leaderstats)
Gold.Name = "Gold"
local Exp = Instance.new("IntValue", leaderstats)
Exp.Name = "Exp"
local Level = Instance.new("IntValue", leaderstats)
Level.Name = "Level"
-- Value update system
local function goldUpdate(updatedValue)
Gold.Value = goldDataStore:Get(updatedValue)
end
local function expUpdate(updatedValue)
Exp.Value = expDataStore:Get(updatedValue)
end
local function levelUpdate(updatedValue)
Level.Value = levelDataStore:Get(updatedValue)
end
goldUpdate(defaultGold)
goldDataStore:OnUpdate(goldUpdate)
expUpdate(defaultExp)
expDataStore:OnUpdate(expUpdate)
levelUpdate(levelUpdate)
levelDataStore:OnUpdate(levelUpdate)
end)
What seems to be the issue?
