Error with Data (string expected, got nil)

For the record, if you want to go that route (which I recommend), you should just be able to do this.

local success, data = pcall(DataStore1.GetAsync, DataStore1, PlayerUserid)

You attempted to save a value called “Name”, but you named the value “NickName”. See if this works:

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 PlayerUserid = "Player_"..player.UserId

	local Data

	local success, errormessage = pcall(function()
		Data = DataStore1:GetAsync(PlayerUserid)
	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 PlayerUserid = "Player_"..player.UserId
	local Data = player.leaderstats.NickName.Value
	
	local success, errormessage = pcall(function()
		DataStore1:SetAsync(PlayerUserid, Data)
	end)
	if success then
		print("Data was saved")
	else 
		print("There was an error")
		warn(errormessage)
	end
end)
1 Like

It seems this worked, I’ll comeback if there’s any errors or futur errors while me adding more things.

Okay, just let me know if there are any errors and I can help.

1 Like

I have a question. How do you change the string value when I edited a TextBox?

(This is why the NickName exists).

Please note it’s in a StarterGUI

I have a script that changes the value whenever you edit the TextBox:

Put this script inside of the TextBox:

local TextBox = script.Parent -- or wherever your TextBox is
local Event = -- Put RemoteEvent here
	
-- LocalScript:
TextBox.InputEnded:Connect(function()
	local Text = tostring(TextBox.Text)
	Event:FireServer(Text)
end)

Put this script in ServerScriptService:

-- ServerScript
local Event = -- Put RemoteEvent here

Event.OnServerEvent:Connect(function(Player, Text)
	local NickName = Player.leaderstats.NickName
	NickName.Value = Text
end)
1 Like

Omg… thank you. Saved me so much time from trying to find an answer. I didn’t found any tutorial on how to change leaderstats.

1 Like

I have one small question, how do you get the TextBox’s text to be the NickName? Since it would be confusing to players.

I think we should do a loop. I tried that but, nothing happens.

Sorry if I’m being annoying.

A TextBox is used for inputting text. Did you mean to say TextLabel?

Ooh, so you can’t change the Text from a TextBox, only in studio and while playing.

Maybe I check if it’s nil and if not check the current value and have a text label on top.

The Text property of a TextBox is the text that the player has inputted into it.

1 Like

So I think I should do this:

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


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

And I’ll modify the TextBox script so whenever it’s used. The TextLabel will be invisible. So what I will do is if the NickName is nil I do nothing. But if it has something I’ll make the TextLabel visible and have it’s text be the NickName. I’ll make it invisible whenever the TextBox is used for a new nickname.

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