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

What's wrong with my leveling system?

Asked by 4 years ago

I seem to be having a problem in making a leveling system in my game which consists of 3 values, Level, Exp and MaxExp. But the problem is, it doesnt seem to be working and also doesnt save and gives me an error:

"19:21:19.295 - 502: API Services rejected request with error. HTTP 403 (Forbidden)"

and doesnt changeal l of the 3 values given and stays 0. And since the MaxExp is also 0, when I use the exp giver it instantly gets me to level 2 and then works normally after that. If you guys have any fixes or have a better leveling system then please let me know. (This is in teamcreate, but I am the owner of the game)

----Variables----
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")

local StoredLevel = DataStoreService:GetDataStore("Level")
local StoredExp = DataStoreService:GetDataStore("Exp")
local StoredMaxExp = DataStoreService:GetDataStore("MaxExp")

----Code----
Players.PlayerAdded:Connect(function(player)

    local Folder = Instance.new("Folder") --creates a new folder in the player
    Folder.Name = "Stats"
    Folder.Parent = player

    local Level = Instance.new("IntValue") --creates a value in the folder
    Level.Name = "Level"
    Level.Parent = player:WaitForChild("Stats")

    local Exp = Instance.new("IntValue") --creates another value in the folder
    Exp.Name = "Exp"
    Exp.Parent = player:WaitForChild("Stats")

    local MaxExp = Instance.new("IntValue")
    MaxExp.Name = "MaxExp"
    MaxExp.Parent = player:WaitForChild("Stats") --creates another value in the folder

    local LevelData
    local ExpData
    local MaxExpData
    --variables to be used later

    local success, errormessage = pcall(function()
        LevelData = StoredLevel:GetAsync(player.UserId)
        ExpData = StoredExp:GetAsync(player.UserId)
        MaxExpData = StoredMaxExp:GetAsync(player.UserId)
        -- setting the variables
    end)

    if success then
        Level.Value = LevelData or 1 --if you dont have a datastore, you get your level to 1
        Exp.Value = ExpData
        MaxExp.Value = MaxExp.Data or 11 --same here             

    else
        print("There Was an Error When Getting Data")
        warn(errormessage)
    end

    ServerStorage.GiveExp.Event:Connect(function(amount, plr) --bindable event to give exp
        if player.Name == plr.Name then
            Exp.Value = Exp.Value + amount
        end
    end)

    Exp.Changed:Connect(function()
        if Exp.Value >= MaxExp.Value then

            local LeftoverExp = MaxExp.Value - Exp.Value
            Exp.Value = 0
            Level.Value = Level.Value + 1
            MaxExp.Value =  10 + Level.Value^3 --this is the level formula
            Exp.Value = Exp.Value - LeftoverExp

        end
    end)
end)


Players.PlayerRemoving:Connect(function(player) -- saves data when player leaves

    local success, errormessage = pcall(function()
        StoredLevel:UpdateAsync(player.UserId, player.Stats.Level.Value)
        StoredExp:UpdateAsync(player.UserId, player.Stats.Exp.Value)
        StoredMaxExp:UpdateAsync(player.UserId, player.Stats.MaxExp.Value)
    end)

    if success then
        print("Player Data Successfully Saved!")
    else
        print("There Was an Error When Saving Data.")
        warn(errormessage)
    end

end)

And this is the exp giver remote event

----Variables----
local ServerStorage = game:GetService("ServerStorage")
local CanGive = true

script.Parent.Touched:Connect(function(hit) -- touched function to give player 5 exp
    if hit.Parent then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            if CanGive == true then
                CanGive = false
                ServerStorage.GiveExp:Fire(5, player)
                wait(1)
                CanGive = true
            end
        end
    end
end)

0
Make sure you allow API access to studio by doing this: go to roblox-->Create--->Click the Cog next to your place and click Configure Game--->In Basic Settings make sure that "Enable Studio Access to API Services:" has a checkmark. SteelMettle1 394 — 4y
0
Wow. That actually worked. I thought I had it turned on but I guess I was wrong. Thanks for the solution. TrueUndefeated 19 — 4y
0
cheers royaltoe 5144 — 4y

Answer this question