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

[Solved] How do i save multiple value with DataStore ?

So i have a Kill and a Money value.

I didn't know how to save multiple values so i got here

So the script looks like this :

local DataStore = game:GetService('DataStoreService')

local UserData = DataStore:GetDataStore('Data')

local Key = 'Player-'

function WhenPlayerJoin(Player)
	local Leader = Instance.new('Folder')
		Leader.Name = 'leaderstats'
		Leader.Parent = Player
		
	local Money = Instance.new('IntValue')
		Money.Name = 'Money'
		Money.Parent = Leader
		
	local Kill = Instance.new('IntValue')
		Kill.Name = "Kill"
		Kill.Parent = Leader
		
	local Data = UserData:GetAsync(Key..Player.UserId)
	
	if Data then
		Money.Value = Data[1] or 0
		Kill.Value = Data[2] or 0
	else
		Data:SetAsync(Key..Player.UserId,{
			Money.Value,	
			Kill.Value
		})
	end
	
	
end

game.Players.PlayerAdded:connect(WhenPlayerJoin)

But i keep getting this error :

 ServerScriptService.DataStore:27: attempt to index local 'Data' (a number value)

1 answer

✓ Accepted answer

Doesn't say a number value says a nil value. Your code doesn't make a lot of sense you said if Data then, (which checks if it's nil), then tried to use it in an else statement. You can't save Data when there's no Data? Also why are you trying to save Data when the player joins, SetAsync should be in a separate function.

Edit: So your code should look like:

local DataStore = game:GetService('DataStoreService')

local UserData = DataStore:GetDataStore('Data')

local Key = 'Player-'

function WhenPlayerJoin(Player)
    local Leader = Instance.new('Folder')
        Leader.Name = 'leaderstats'
        Leader.Parent = Player

    local Money = Instance.new('IntValue')
        Money.Name = 'Money'
        Money.Parent = Leader

    local Kill = Instance.new('IntValue')
        Kill.Name = "Kill"
        Kill.Parent = Leader

    local Data = UserData:GetAsync(Key..Player.UserId) 

    if Data then
        Money.Value = Data[1] 
        Kill.Value = Data[2]
    else
        Money.Value = 0
        Kill.Value = 0
    end

for i, child in pairs(Leader:GetChildren()) do
	child.Changed:connect(function()
	UserData:SetAsync(Key..Player.UserId,{Money.Value,Kill.Value})
	print("Data Saved")
	end)
end
end

function PlayerLeaving(Player)
	local Money = Player.leaderstats.Money
	local Kill = Player.leaderstats.Kill
	print("The stats are there")
	UserData:SetAsync(Key..Player.UserId,{Money.Value,Kill.Value})
	print("Data Saved")
end
game.Players.PlayerAdded:connect(WhenPlayerJoin)
game.Players.PlayerRemoving:connect(PlayerLeaving)

Edit I really reccomend using pcall in case of any DataLoadingErrors: http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#pcall

3 comments

0

Thanks

0

In your for loop, putting events in loops is a bad practice. Instead make a changed event and clone the script into the leaderstats children. and make disabled to false

0

Just wondering, why is events in loops a bad practice?

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