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

Error using DataStore when there is no value to save.. Help?

I don't know why I am getting the following error on the line marked on the script: Bad argument #3 to "Name (string expected,got nil) What is this and why does it happen? Is there anything wrong with the script or?

game.Players.PlayerAdded:connect(function(player)
	   local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Skins")
	repeat wait() until player.Character
	
	player.Character:WaitForChild("PlayerSkins")
	wait(1)
	local stats = player.Character.PlayerSkins:GetChildren()
	player.Character.PlayerSkins.Script.AddValue:Fire()
	
	for i = 1, #stats do 
	stats[i].Name = datastore:GetAsync(stats[i].Name) --Error here.
	print(stats[i].Name.. "Loaded")
	end
end)

1 answer

So by the title and error, I'm assuming you're getting an error because there's nothing in the table, making it empty or nil. If stats[i] is empty or nil, the script will create an error because you can't get the name of nothing. :P

You could replace lines 7-13 with this to fix your script :

local stats = player.Character.PlayerSkins:GetChildren()
player.Character.PlayerSkins.Script.AddValue:Fire()
for i = 1, #stats do
	if stats[i] then --Checks if there is something or not
 		stats[i].Name = datastore:GetAsync(stats[i].Name)
		print(stats[i].Name.. "Loaded")
	end
end

I used conditional statements so the script can check whether there's something in the table or not. If there is, it prints the part's name, otherwise nothing will happen.

Hope this helped! :D

1 comment

0

I still get the same error :( But thank you for trying!

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