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

Is playerTable[player].Exp on my script a nill? how can i fix it?

Asked by 3 years ago

So I've finished my other problem concerning about client to server and back....

now I encountered a new problem..

my First script

local datastore = game:GetService("DataStoreService")
local mydatastore = datastore:GetDataStore("DataStorage")

local playerTable = {}
local rp = game.ReplicatedStorage
-- save
game.Players.PlayerAdded:Connect(function(player)


    playerTable[player] = {Level = 0,Exp = 0,Inventory = 0}
    local currentlevel = mydatastore:GetAsync("savedlevel"..player.UserId)
    local currentexp = mydatastore:GetAsync("savedexp"..player.UserId)
    local currentinventory = mydatastore:GetAsync("savedinventory"..player.UserId)


    playerTable[player].Level = currentlevel
    playerTable[player].Exp = currentexp
    playerTable[player].Inventory = currentinventory
    wait(1)

    player.PlayerGui.Exp.Frame.xpValue.Text = playerTable[player].Exp

end)
--save

Below the first script when the remote is triggered

rp.sCrystalEXP.OnServerEvent:Connect(function(player, key)
    if key == "SCT" then
        print("Aquired")
        playerTable[player].Exp = playerTable[player].Exp + 1
        wait(0.6)
        if playerTable[player].Inventory == 0 then
            player.PlayerGui.Exp.Frame.xpValue.Text = playerTable[player].Exp.."/"..10
        end
    end
end)

Whenever I test it it says

ServerScriptService.General Script:31: attempt to perform arithmetic (add) on nil and number

which is

playerTable[player].Exp = playerTable[player].Exp + 1

How can i fix this?

0
I believe it is because you have no data saved. If you don't save the data anytime or if a new player joins, I think all your values in your table will be nil Spjureeedd 385 — 3y
0
How can I save the data? MachoMasterKirby 2 — 3y
0
put a leaderstats WINDOWS10XPRO 438 — 3y
0
You are using DataStoreService to get saved data without first saving any. If you don't know how to save data then you should read up on DataStoreService Spjureeedd 385 — 3y

Answer this question