Exp system error

I took a youtuber’s script for a level system and I don’t really understand how to fix the errors, please help me

local level = 1
local exp = 0

local axp = 20

local InStudio = game:GetService(“RunService”):IsStudio()

if not InStudio then

level = game:GetService("DataStoreService"):GetDataStore("Levels",1)

exp = game:GetService("DataStoreService"):GetDataStore("EXP",1)

axp = game:GetService("DataStoreService"):GetDataStore("AXP",1)
end

function savedata(dataname, playerid, value)

		dataname:SetAsync(playerid, value)
end

game.Players.PlayerAdded:connect(function(player)

local leader = Instance.new("Folder")

leader.Name = "leaderstats"

leader.Parent = player

local levelz = Instance.new("IntValue")

levelz.Name = "Level"

local xpz = Instance.new("NumberValue")

xpz.Name = "Exp"

local xpn = Instance.new("IntValue")

xpn.Name = "ExpNeeded"


if not InStudio then

    xpn.Value = axp:GetAsync(tostring(player.userId)) or 20

    xpz.Value = exp:GetAsync(tostring(player.userId)) or 0

    levelz.Value = level:GetAsync(tostring(player.userId)) or 1
	
	    
else

    xpn.Value = axp

    xpz.Value = exp

    levelz.Value = level

end


levelz.Parent = leader
xpz.Parent = leader
xpn.Parent = leader


xpz.Changed:connect(function()

    if player.leaderstats:WaitForChild("Exp").Value >= player.leaderstats:WaitForChild("ExpNeeded").Value then

        levelz.Value = levelz.Value + 1



        

        xpn.Value = math.floor(xpn.Value * 2)

        xpz.Value = 0   

        

        savedata(level, player.userId, levelz.Value)

        savedata(exp, player.userId, xpz.Value)

        savedata(axp, player.userId, xpn.Value)

    else

end
end)

end)

game.Players.PlayerRemoving:connect(function(player)

savedata(level, player.userId, player.leaderstats.Level.Value)

savedata(exp, player.userId, player.leaderstats.Exp.Value)

savedata(axp, player.userId, player.leaderstats.ExpNeeded.Value)
end)

1 Like

You didn’t check if you were in studio before trying to save. Because of the way you’ve set this up, if you’re in studio the values are set to numbers instead of to DataStores.

1 Like

in game the game save the values but on studio, the output displays errors

1 Like

Correct. Because you don’t have a check in there to see if you are in studio or not. See how elsewhere in the code they check not InStudio before doing any DataStore calls.

You need the same for the saving part.

2 Likes

Thank you very much, I am new to coding and I did not know

my game:

https://www.roblox.com/games/5378846807/PLANET-51-V1