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

(data saving) string expected got nil?data store wont save my string value

i think im using the data store wrong

local DSService = game:GetService("DataStoreService"):GetDataStore("Level")
game.Players.PlayerAdded:connect(function(plr)
	local Ukey = "id-"..plr.UserId
	local leaderstat = Instance.new("IntValue",plr)
	leaderstat.Name = "leaderstats"
	local LevelValue = Instance.new("IntValue",leaderstat)
	LevelValue.Parent = leaderstat
	LevelValue.Name = "Level"
	local Sp = Instance.new("StringValue",leaderstat)
	Sp.Parent = leaderstat
	Sp.Name = "Power"
	Sp.Value = ""
	local GetSaved = DSService:GetAsync(Ukey)
	if GetSaved then
	LevelValue.Value = GetSaved[1]
	Sp.Value = GetSaved[2]
	else
	local NumbersForSaving = {LevelValue.Value,Sp.Value}
	DSService:SetAsync(Ukey,NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local Ukey = "id-"..plr.UserId
	local Savetable = {plr.leaderstats.Level.Value,plr.leaderstats.Sp.Value}
	DSService:SetAsync(Ukey,Savetable)
end)

3 answers

✓ Accepted answer

You never give Sp a value so it is attempting to Get nil and make it the value of your StringValue which cannot be done. It appears Sp never has a value change meaning this never works.

First off, you are attempting to save two values to the same key. This won't work, as one will overwrite the other. Second, a better way might be to use a dictionary table, in case you have lots of values and it takes a while to find them all. This code should theoretically work.

local DSService = game:GetService("DataStoreService"):GetDataStore("Level")
game.Players.PlayerAdded:connect(function(plr)
    local Ukey = "id-"..plr.UserId
    local leaderstat = Instance.new("IntValue",plr)
    leaderstat.Name = "leaderstats"
    local LevelValue = Instance.new("IntValue")
    LevelValue.Parent = leaderstat
    LevelValue.Name = "Level"
    local Sp = Instance.new("StringValue") ------ this part
    Sp.Parent = leaderstat
    Sp.Name = "Power" ------- until this part
    local GetSaved = DSService:GetAsync(Ukey)
    if GetSaved then
        LevelValue.Value = GetSaved.lvl
        Sp.Value = GetSaved.pwr ----think the problem lies here
    else
        local svtbl = {lvl = LevelValue.Value, pwr = Sp.Value}
        DSService:SetAsync(Ukey,svtb1)
    end
end)


    
game.Players.PlayerRemoving:connect(function(plr)
    local Ukey = "id-"..plr.UserId
    local Savetable = {lvl = plr.leaderstats.Level.Value, pwr = plr.leaderstats.Sp.Value}
    DSService:SetAsync(Ukey,Savetable)
end)

1 comment

0

sorry but this didnt help and it didnt even store the values in the datastore or didnt retrieve it and eitherway it still says that line 15 has a error

Try storing all your values in one table, rather than 2. Try this:

local DSService = game:GetService("DataStoreService"):GetDataStore("Level")
game.Players.PlayerAdded:connect(function(plr)
    local Ukey = "id-"..plr.UserId
    local leaderstat = Instance.new("IntValue",plr)
    leaderstat.Name = "leaderstats"
    local LevelValue = Instance.new("IntValue")
    LevelValue.Parent = leaderstat
    LevelValue.Name = "Level"
    local Sp = Instance.new("StringValue") ------ this part
    Sp.Parent = leaderstat
    Sp.Name = "Power" ------- until this part
    local GetSaved = DSService:GetAsync(Ukey)
    if GetSaved then
    LevelValue.Value = GetSaved[1]
    Sp.Value = GetSaved[2] ----think the problem lies here
    else
    local NumbersForSaving = {LevelValue.Value,Sp.Value}
    DSService:SetAsync(Ukey,NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local Ukey = "id-"..plr.UserId
    local Savetable = {plr.leaderstats.Level.Value,plr.leaderstats.Sp.Value}
    DSService:SetAsync(Ukey,Savetable)
end)

I also recommend that you save the player's data occasionally because sometimes servers shut down randomly and can cause data loss.

1 comment

0

i think you didnt get my question and sorry for the confusion but the problem lies in line 15 cause the output says string expected got nil

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