Error with Data (string expected, got nil)

I’m not sure if this is what you’re trying to do, but see if this works:

local TextLabel = script.Parent.TextLabel
local TextBox = script.Parent.TextBox

game.Players.PlayersAdded:Connect(function(player)
	local NickName = player:WaitForChild("leaderstats").NickName
	if NickName.Value == "" then
		TextLabel.Visible = false
	else
		TextLabel.Visible = true
		TextLabel.Text = NickName.Value
	end
end)

I got it. I made a few modifications but it worked! Thanks a lot. From one simple qestion we went on many.

Here’s my script: (bit messy in a way)

game.Players.PlayerAdded:Connect(function(player)
	local GUI = player.PlayerGui:WaitForChild("PlayersCard").Frame.NNFrame
	local TextLabel = GUI.Default
	local TextBox = GUI.ChangeNickName
	print("PLAYER JOINED")
	print(player.Name.."is crazy") -- just to check for usernames. I usually print funny stuff
	local NickName = player:WaitForChild("leaderstats").NickName
	wait(1)
	print(NickName.Value)
	if NickName.Value == "" or nil then
		print("There is no text")
		TextLabel.Visible = false
	else
		print("There is text")
		TextLabel.Visible = true
		TextLabel.Text = NickName.Value
	end
end)
1 Like

I ran into an issue. Data sometimes saves, rarily but sometimes.

Edit: I think it’s fixed now. Phew

1 Like

Just in case it stops working, try this script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Name = Instance.new("StringValue")
	Name.Name = "NickName"
	Name.Parent = leaderstats

	local success, errormessage
	
	success, errormessage = pcall(function()
		Data = DataStore1:GetAsync(player.UserId)
	end)
	
	if success then
		if Data ~= nil then
			Name.Value = Data
		else
			Name.Value = player.Name
		end
	else
		print(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Data = player.leaderstats.NickName.Value
	local success, errormessage

	success, errormessage = pcall(function()
		DataStore1:SetAsync(player.UserId, Data)
	end)
	if success then
		print("Data was saved")
	else 
		print("There was an error")
		print(errormessage)
	end
end)

Btw, I change it from “Player_…player.UserId” to just the player’s UserId, so it will reset the data.

1 Like

I’ll soon (maybe tomorrow) add more values. I’ll comeback if there’s any problems.

It happened AGAIN. IT’S SO WEIRD. I NEVER TOUCH THE SCRIPTS WHILE THEY WERE WORKING AND NOW THEY BARELY SAVE. So weird. I’ll make another post asking for help. Since this one has the initial solution and people don’t go on it.

1 Like

@NeonTaco135 @JackscarIitt @JarodOfOrbiter @Aanggoodluck @ComplexMetatable
I made a post about my data not saving if you’re interested in helping.

Help with DataStore not saving/working (PlayerRemoving/game:BindToClose) - Help and Feedback / Scripting Support - DevForum | Roblox