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 nil with 'Strength'?

For some context, its a server script and its in serverscriptservice. heres a snippet of the code where the error is, along with the entire script (i have honestly no idea whats wrong with it

	if success then
		strength.Value = data.Strength -- this is the error
	else

the entire script

local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData7")

game.Players.PlayerAdded:Connect(function(player)

	local data2 = Instance.new("Folder")
	data2.Name = "Data2"
	data2.Parent = player

	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Value = 0
	strength.Parent = data2

	local playerUserId = "Player_"..player.UserId

	local data
	local success , errormessage = pcall(function()
		data = SaveDataStore:GetAsync(playerUserId)
	end)
	
	print("2242")
	if success then
		strength.Value = data.Strength
	else
		print("there was an error while saving your data.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		strength = player.Data.Strength.Value
	}

	local success,errormessage = pcall(function()
		SaveDataStore:SetAsync(playerUserId,data)
	end)	
	if success then
		print("data saved gg ez")
	else
		print("there was an error")
		warn(errormessage)
	end
end)

2 answers

If you join for the first time, no data is saved in the data store so data is nil no matter what, all you have to do is check if the player has not joined for the first time, as easy as this:

...
local data
local success , errormessage = pcall(function()
	data = SaveDataStore:GetAsync(playerUserId)
end)

print("2242")
if success then
	if (data) then
		strength.Value = data.Strength
	else
		print('Gamer has no data saved')
	end
else
	...

The other possible problem is that you are saving the data on PlayerRemoving but if game closes before PlayerRemoving saves the data, nothing will save, this is why you use game:BindToClose, it binds given function and the server will wait before the function finished before closing, all you have to do is also save user data inside of the BindToClose function as server will wait before the data is saved.

So would it be something like this?

game:BindToClose(function()
	local playerUserId = "Player_"..player.UserId

	local data = {
		strength = player.Data.Strength.Value
	}

	local success,errormessage = pcall(function()
		SaveDataStore:SetAsync(playerUserId,data)
	end)    
	if success then
		print("data saved gg ez")
	else
		print("there was an error")
		warn(errormessage)
	end
end)

1 comment

0

yeah although you need to run a loop for each player in the server as player variable is not defined, do same but with ipairs loop for game:GetService('Players'):GetPlayers()'

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