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

How To Properly Use DataStoreService For My Levelling System?

Asked by 1 year ago

Hello! I've recently been wondering how to properly use the DataStoreService for my leveling system. There are no script errors but whenever I test my game the script doesn't work. I have a gui that display's the players xp and level, but when testing my game the xp and level do not show up, the only thing that shows up is the two blank text labels. Also, the leaderstats do not show up either, it's just a leaderboard that states my username. I am wanting my script to be whenever the player achieves new levels and gains xp in game, whenever they join back later on their level and xp remain just as it was when they left and knowing the errors in my script would help me out a ton. I am just recently getting to learn data storage's and I am currently stuck. Any help is appreciated!!

local DataStoreService = game:GetService("DataStoreService")
local levelDataStore = DataStoreService:GetDataStore("levelDataStore")
local xpDataStore = DataStoreService:GetDataStore("xpDataStore")

function onXPChanged(player, XP, level)
    if XP.Value>=level.Value * 10 then 
        XP.Value = 0 
        level.Value = level.Value + 1 
    end
end

function onLevelUp(player, XP, level)
    local m = Instance.new("Hint")
    m.Parent = game.Workspace
    m.Text = player.Name .. " has leveled up!"
    wait(3)
    m.Parent = nil
   player.Humanoid.Health = 0
end


function onPlayerRespawned(player)
    wait(5)

    player.Character.Humanoid.Health = player.Character.Humanoid.Health + player.leaderstats.Level * 10

    player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + player.leaderstats.Level * 10

end

function onPlayerEntered(newPlayer, player)

    local stats = Instance.new("IntValue")
    stats.Name = "leaderstats"
    local stats2 = Instance.new("IntValue")
    stats2.Name = "Tycoon"

   local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    kills.Value = 0

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Value = 0

   local level = Instance.new("IntValue")
   level.Name = "Level" -- The Name of LV
    level.Value = levelDataStore:GetAsync("Player_"..player.UserId) or 1

   local xp = Instance.new("IntValue")
   xp.Name = "XP" -- The Name of XP
    xp.Value = xpDataStore:GetAsync("Player_"..player.UserId) or 0
    local data
    local success, errormessage = pcall(function()
        data = levelDataStore:GetAsync(player.UserId.."-xp")
        data = xpDataStore:GetAsync(player.UserId.."-level")
    end)
    if success then
        level.Value = data
        xp.Value = data
    else
        print("There was a problem loading your data.")
        warn(errormessage)
    end

    game.Players.PlayerRemoving:Connect(function(player)
        local s, e = pcall(function()
            levelDataStore:SetAsync("Player_"..player.UserId, player.Level.Value)
            xpDataStore:SetAsync("Player_"..player.UserId, player.Level.Current.Value)
        end)
        if s then
            print("Successfully saved data!")
        else
            print("There was an error when saving data.")
        end
    end)

    stats2.Parent = newPlayer   
    stats.Parent = newPlayer
    kills.Parent = stats
    deaths.Parent = stats
   level.Parent = stats
   xp.Parent = stats

    xp.Changed:connect(function() onXPChanged(newPlayer, xp, level) end)
    level.Changed:connect(function() onLevelUp(newPlayer, xp, level) end)

    while true do
        if newPlayer.Character ~= nil then break end
        wait(5)
    end

    local humanoid = newPlayer.Character.Humanoid

    humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )


    newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )


    stats.Parent = newPlayer

end

function Send_DB_Event_Died(victim, killer)

    local killername = "unknown"
    if killer ~= nil then killername = killer.Name end
    print(victim.Name, " was killed by ", killername)

    if shared["deaths"] ~= nil then 
        shared["deaths"](victim, killer)
        print("Death event sent.")
    end
end

function Send_DB_Event_Kill(killer, victim)
    print(killer.Name, " killed ", victim.Name)
    if shared["kills"] ~= nil then 
        shared["kills"](killer, victim)
        print("Kill event sent.")
    end
end



function onHumanoidDied(humanoid, player)
    local stats = player:findFirstChild("leaderstats")
    if stats ~= nil then
        local deaths = stats:findFirstChild("Deaths")
        deaths.Value = deaths.Value + 1


        local killer = getKillerOfHumanoidIfStillInGame(humanoid)


        Send_DB_Event_Died(player, killer)
        handleKillCount(humanoid, player)
    end
end

function onPlayerRespawn(property, player)


    if property == "Character" and player.Character ~= nil then
        local humanoid = player.Character.Humanoid
            local p = player
            local h = humanoid
            humanoid.Died:connect(function() onHumanoidDied(h, p) end )
    end
end

function getKillerOfHumanoidIfStillInGame(humanoid)

    local tag = humanoid:findFirstChild("creator")


    if tag ~= nil then

        local killer = tag.Value
        if killer.Parent ~= nil then 
            return killer
        end
    end

    return nil
end

function handleKillCount(humanoid, player)
    local killer = getKillerOfHumanoidIfStillInGame(humanoid)
    if killer ~= nil then
        local stats = killer:findFirstChild("leaderstats")
        if stats ~= nil then
            local kills = stats:findFirstChild("Kills")
         local xp = stats:findFirstChild("XP")
            if killer ~= player then
                kills.Value = kills.Value + 1
            xp.Value = xp.Value + 3

            else
                kills.Value = kills.Value - 0

            end
            Send_DB_Event_Kill(killer, player)
        end
    end
end

game.Players.ChildAdded:connect(onPlayerEntered)
0
Players.ChildAdded or Players.PlayerAdded only sends one value. Your onPlayerEntered has 2 (newPlayer, player). 9mze 193 — 1y
0
Players.PlayerAdded only sends one variable. The only variable of PlayerAdded is the player. Your onPlayerEntered has 2 variables (newPlayer, player) (Line 31). 9mze 193 — 1y
0
yes and I did infact change that to just one value. But whenever I load into my game and test it out, the entire script freezes my game and whenever I kill someone the gui doesn't change and the player doesn't get his XP. AROBLOXUSER12344 24 — 1y

Answer this question