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?

4 comments

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

0

How can I save the data?

0

put a leaderstats

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