Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

String expected, got nil. How to fix?

Asked by 4 years ago

Hello! I am trying to make a DataStore script for keybinds in my game. I have string values saved in a table. When I test the script, I get an error: Invalid argument #3 (string expected, got nil) It is on line 29 - 33.

local ds = game:GetService("DataStoreService")
local keys = ds:GetDataStore("KeySave")

game.Players.PlayerAdded:Connect(function(player)
    local playerFolder = Instance.new("Folder", game.ReplicatedStorage)
    playerFolder.Name = player.Name

    local keybindFolder = Instance.new("Folder", playerFolder)
    keybindFolder.Name = "Keybinds"

    local stopKeyBind = Instance.new("StringValue", keybindFolder)
    stopKeyBind.Name = "stopKeyBind"

    local readyKeybind = Instance.new("StringValue", keybindFolder)
    readyKeybind.Name = "readyKeyBind"

    local lPunch = Instance.new("StringValue", keybindFolder)
    lPunch.Name = "lPunchKeyBind"

    local rPunch = Instance.new("StringValue", keybindFolder)
    rPunch.Name = "rPunchKeyBind"

    local fightingKeyBind = Instance.new("StringValue", keybindFolder)
    fightingKeyBind.Name = "fightingPositionKeyBind"

    local keyStats = keys:GetAsync(player.UserId)

    if keyStats ~= nil then
        stopKeyBind.Value = keyStats[1]
        readyKeybind.Value = keyStats[2]
        lPunch.Value = keyStats[3]
        rPunch.Value = keyStats[4]
        fightingKeyBind.Value = keyStats[5]
    else
        stopKeyBind.Value = "p"
        readyKeybind.Value = "r"
        lPunch.Value = "q"
        rPunch.Value = "e"
        fightingKeyBind.Value = "f"
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local save = {}

    table.insert(save, game.ReplicatedStorage[player.Name].Keybinds.stopKeyBind.Value)
    table.insert(save, game.ReplicatedStorage[player.Name].Keybinds.readyKeyBind)
    table.insert(save, game.ReplicatedStorage[player.Name].Keybinds.lPunchKeyBind.Value)
    table.insert(save, game.ReplicatedStorage[player.Name].Keybinds.rPunchKeyBind.Value)
    table.insert(save, game.ReplicatedStorage[player.Name].Keybinds.fightingPositionKeyBind.Value)
    keys:SetAsync(player.UserId,game:GetService("HttpService"):JSONEncode(save))
end)

Any help would be appreciated. Thanks!

2 answers

Log in to vote
0
Answered by 4 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

You should probably save it in a table then load that table. Something like this:

01 local ds = game:GetService("DataStoreService") 02 local keys = ds:GetDataStore("KeySave") 03
04 game.Players.PlayerAdded:Connect(function(player) 05 local playerFolder = Instance.new("Folder", game.ReplicatedStorage) 06 playerFolder.Name = player.Name 07
08 local keybindFolder = Instance.new("Folder", playerFolder) 09 keybindFolder.Name = "Keybinds" 10
11 local stopKeyBind = Instance.new("StringValue", keybindFolder) 12 stopKeyBind.Name = "stopKeyBind" 13
14 local readyKeybind = Instance.new("StringValue", keybindFolder) 15 readyKeybind.Name = "readyKeyBind" 16
17 local lPunch = Instance.new("StringValue", keybindFolder) 18 lPunch.Name = "lPunchKeyBind" 19
20 local rPunch = Instance.new("StringValue", keybindFolder) 21 rPunch.Name = "rPunchKeyBind" 22
23 local fightingKeyBind = Instance.new("StringValue", keybindFolder) 24 fightingKeyBind.Name = "fightingPositionKeyBind" 25
26 local keyStats

local success,errorMsg = pcall(function() keyStats = keys:GetAsync("keyBinds-"..player.UserId) end) 27
28 if success then

    if keyStats then

        for x = 1,#keyStats do

        local newKeyBind = Instance.new("StringValue")
        newKeyBind.Name = keyStats[x]
        newKeyBind.Parent = player.KeyBinds

        wait()
    end
end

end 41 end) 42
43 game.Players.PlayerRemoving:Connect(function(player) 44 local savedKeyBinds = {}

for _,keyBind in pairs(player.Keybinds) do table.insert(savedKeyBinds,keyBind.Name)

wait() end

local success,errorMsg = pcall(function() keys:SetAsync("keyBinds-"..player.UserId,savedKeyBinds) end)

52 end)

Ad
Log in to vote
0
Answered by
7zjh 146
4 years ago

Its erroring because the table you are referencing does not exist so Lua automatically says its nil. You need to have a table inside the script before referencing the supposed table.

Answer this question