Hello!
While creating my game. I needed some Data. This was my first time using Data.
While creating Data I ran into an issue:
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.Value = player.Name
Name.Parent = leaderstats
print(Name.Value)
local PlayerUserid = "Player_"..player.UserId
print(PlayerUserid)
local Data
local success, errormessage = pcall(function()
Data = DataStore1:GetAsync(PlayerUserid)
end)
if success then
-- this is where I ran into an issue
Name.Value = Data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserid = "Player_"..player.UserId
local Data = player.leaderstats.Name.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)
The Output said this:
ServerScriptService.Data:23: invalid argument #3 (string expected, got nil)
Does anyone know what I did wrong?
Thanks for reading.
If it’s how I named Name
then please tell me to change the name.
Data is nil, likely because there is no save data yet. Check if it is nil, and if it is you should set Name.Value to a default value like 0 or “TextHere” or something.
That wouldn’t actually work since his function doesn’t return a value. He sets the variable, which is fine.
1 Like
local success, data= pcall(function()
return DataStore1:GetAsync(PlayerUserid)
end)
if success then
if data then
Name.Value = data
else
Name.Value = "No Name"
end
else
error("connection error")
end
Try adding another if statement/adding additional statement to the current checking whether Data exists or not.
Supposedly you could just check if both success
& Data
are valid variables, & if 1 of them returns back as false
or invalid, you can just set its value to what its default is supposed to be using an else
statement
if success and Data then
Name.Value = Data
else
warn(errormessage)
end
1 Like
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.