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

"attempt to index a number value" DataStore?

local DataStoreService=game:GetService("DataStoreService")
local DataStore=DataStoreService:GetDataStore("Player_Data")
game.Players.PlayerAdded:connect(function(Player)
	
	local gameData; 	
	local Stats=Instance.new('Folder')
	Stats.Name="plrData"
	local Money=Instance.new('NumberValue',Stats)
	Money.Name="Credits"
	Stats.Parent=Player
	local SpecialMoney=Instance.new('NumberValue',Stats)
	SpecialMoney.Name = "Resort Tokens"
	local ownedWeapons=Instance.new('Folder',Stats)
	ownedWeapons.Name = "ownedWeapons"
	

	

	local CanSave=Instance.new('BoolValue',Player) 
	                                               
	CanSave.Name="CanSaveData"
	CanSave.Value=true
	
	local DataFetchSuccess,ErrorMessage=pcall(function()  
	PlayerData=DataStore:GetAsync(tostring(Player.UserId))
	end)
	
	if DataFetchSuccess then 
		
		if PlayerData~=nil then 
			PlayerData:Clone().Parent = Player
		else 
			Player.plrData.Credits.Value=100 	
		end
		
	else 
		
	
		Player.CanSaveData.Value=false
		Player:Kick("[SERVER] DS002 (Check the Manual for more information.)")	
			
	end
  
end)
game.Players.PlayerRemoving:connect(function(Player)
	
	if Player.CanSaveData.Value==false then return end 
	
	local PlayerData=Player.plrData
	
	local DataWriteSuccess,ErrorMessage=pcall(function() 
		DataStore:SetAsync(tostring(Player.UserId),PlayerData)
	end)	
	
	if not DataWriteSuccess then 
		
		local Retry_Count=0
		
		while Retry_Count<6 do
			wait(60) 
			local Succeded,Error=pcall(function()
				DataStore:SetAsync(tostring(Player.UserId),PlayerData)
			end)
			if Succeded then break end 
			Retry_Count=Retry_Count+1
		end
		
	end
	
end)

On Line 31, it gives me the title error. How could I clone the player's data if they played already?

1 answer

As the error said you're trying to index a number. PlayerData=DataStore:GetAsync(tostring(Player.UserId) returns a number, not a NumberValue object. You can't call Clone on a number. Perhaps you meant to do Player.plrData.Credits.Value = PlayerData

6 comments

0

How do I clone the folder from the DataStore to the player?

0

The DataStore didn't contain a folder, if you have a folder then you can use Clone. But in this case you had a number. You can't save objects like folders in DataStore.

0

Well how could I save plrData?

0

Save it as something else and not a folder, a table perhaps or a key for each value.

0

I want to use a table. How do I use tables with DataStores? (sorry for excessive asking xd)

0

You just use them... Try having a look at the wiki for datastore and table if you're unfamiliar with them.

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