Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Using DataStore2 To Save Magic Values As A "Nil" Value?

So, I am trying to save a magic level inside of a folder inside of a player using DataStore2, and it's giving me the error that magicLevel is a nilValue?

This Is A Server Script Inside Of ServerScriptService

local defaultMagicLevel = 1
local DataStore2 = require(1936396537)

local MainKey = "MainKey"

DataStore2.Combine(MainKey, "Stats")

local function SetDataTable()
	local userData = {
		Stats = {
			magicLevel = 1
		}
	}
	return userData
end

game.Players.PlayerAdded:Connect(function(plr)
	local userData = DataStore2(MainKey,plr):Get(SetDataTable())
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local magicLevel = Instance.new("IntValue")
	magicLevel.Value = defaultMagicLevel
	magicLevel.Name = "Level"
	
	local statsData = DataStore2("Stats",plr)
	
	local function updateStats(updatedValue)
		magicLevel.Value = statsData:Get(updatedValue).magicLevel
	end
	
	updateStats(userData.Stats)
	
	statsData:OnUpdate(updateStats)
	
	leaderstats.Parent = plr
	magicLevel.Parent = leaderstats
	
	userData.Stats.magicLevel = userData.magicLevel + 1
	statsData:Set(userData.Stats)
end)

Error:

21:43:55.602 - ServerScriptService.Stats:39: attempt to perform arithmetic on field 'magicLevel' (a nil value) 21:43:55.603 - Stack Begin 21:43:55.604 - Script 'ServerScriptService.Stats', Line 39 21:43:55.605 - Stack End

Any Database Wizards?

1 comment

0

This isn't free model (DS2) support

1 answer

This discussion is preserved as it was. New answers, comments, and votes are closed.