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

[SOLVED]My level up system wont work after switching to data storage? [closed]

Asked by
soutpansa 120
7 years ago
Edited 7 years ago

My game used to use Data Persistence, but as you know, Data Persistence is unreliable and you cant have different "universes" etc. My level up system's functions are integrated into the Data Persistence script. But when I try to use it with data storage, it wont level me up. And I am not getting any errors. I will post the Level code first, the level code with Data Persistence second, and the Data Storage code last. Please help if you've got any ideas ^^

Level Code:

function onXPChanged(player, XP, level) 
    if XP.Value>=level.Value * 20 then 
    XP.Value = XP.Value - level.Value * 20 
    level.Value = level.Value + 1
    player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 10
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 10
    player.Levelss.FAP.Value = player.Levelss.FAP.Value + 1
    end 
end 


function onLevelUp(player, XP, level) 
    if player.Character~=nil then 
    for i = 1,5 do 
    local fireworks = Instance.new("Part") 
    fireworks.Shape = 0 
    fireworks.Material = "Neon"
    fireworks.formFactor = "Symmetric" 
    fireworks.Size = Vector3.new(1,1,1) 
    fireworks.BrickColor = BrickColor.Random() 
    fireworks.CFrame = player.Character.Head.CFrame + Vector3.new(0,2,0) 
    fireworks.Parent = game.Workspace 
    local fu = Instance.new("Fire")
    fu.Size = 5
    fu.Heat = 5
    fu.Color = Color3.new(math.random(),math.random(),math.random()) 
    fu.SecondaryColor = Color3.new(math.random(), math.random(), math.random())
    fu.Parent = fireworks 
    game:GetService("Debris"):AddItem(fireworks, 2) 
    fireworks.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30)) 
        end 
    end 
end 

function onPlayerEntered(newPlayer)
    newPlayer.CharacterAdded:connect(function(c) 
    c.Humanoid.MaxHealth = newPlayer.Levelss.Level.Value * 10 + 100
    c.Humanoid.Health = newPlayer.Levelss.Level.Value * 10 + 100 
    end) 
end


game.Players.PlayerAdded:connect(onPlayerEntered)


Data Persistence with Level Code:

function onXPChanged(player, XP, level) 
    if XP.Value>=level.Value * 20 then 
    XP.Value = XP.Value - level.Value * 20 
    level.Value = level.Value + 1
    player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 10
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 10
    player.Levelss.FAP.Value = player.Levelss.FAP.Value + 1
    end 
end 

function onLevelUp(player, XP, level) 
    if player.Character~=nil then 
    for i = 1,5 do 
    local fireworks = Instance.new("Part") 
    fireworks.Shape = 0 
    fireworks.Material = "Neon"
    fireworks.formFactor = "Symmetric" 
    fireworks.Size = Vector3.new(1,1,1) 
    fireworks.BrickColor = BrickColor.Random() 
    fireworks.CFrame = player.Character.Head.CFrame + Vector3.new(0,2,0) 
    fireworks.Parent = game.Workspace 
    local fu = Instance.new("Fire")
    fu.Size = 5
    fu.Heat = 5
    fu.Color = Color3.new(math.random(),math.random(),math.random()) 
    fu.SecondaryColor = Color3.new(math.random(), math.random(), math.random())
    fu.Parent = fireworks 
    game:GetService("Debris"):AddItem(fireworks, 2) 
    fireworks.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30)) 
        end 
    end 
end 


function saveScore(player, score)
    player:SaveNumber("Level", score)
end

function saveScore2(player, score)
    player:SaveNumber("XP", score)
end

function saveScore3 (player, score)
    player:SaveNumber("FAP", score)
end

function saveScore4 (player, score)
    player:SaveNumber("RandomMove", score)
end

function saveScore5 (player, score)
    player:SaveNumber("Muny", score)
end

function saveScore6 (player, score)
    player:SaveNumber("RangedPunch", score)
end



function loadScore(player, clickCounter)
    local score = player:LoadNumber("Level") 

    if score ~= 1 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore2(player, clickCounter)
    local score = player:LoadNumber("XP") 

    if score ~= 0 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore3(player, clickCounter)
    local score = player:LoadNumber("FAP") 

    if score ~= 0 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore4(player, clickCounter)
    local score = player:LoadNumber("RandomMove") 

    if score ~= 0 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore5(player, clickCounter)
    local score = player:LoadNumber("Muny") 

    if score ~= 0 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore6(player, clickCounter)
    local score = player:LoadNumber("RangedPunch") 

    if score ~= 0 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end



function onPlayerEntered(newPlayer)



        local stats = Instance.new("IntValue")
        stats.Name = "Levelss"

        local clicks = Instance.new("IntValue")
        clicks.Name = "Level"
        clicks.Value = 1

        local clicks2 = Instance.new("IntValue")
        clicks2.Name = "XP"
        clicks2.Value = 0

        local clicks3 = Instance.new("IntValue")
        clicks3.Name = "FAP"
        clicks3.Value = 1

        local clicks4 = Instance.new("IntValue")
        clicks4.Name = "RandomMove"
        clicks4.Value = 0

        local clicks5 = Instance.new("IntValue")
        clicks5.Name = "Muny"
        clicks5.Value = 250

        local clicks6 = Instance.new("IntValue")
        clicks6.Name = "RangedPunch"
        clicks6.Value = 0

        local clicks1337 = Instance.new("IntValue")
        clicks1337.Name = "PunchDamageIncrease"
        clicks.Value = 0

        clicks.Parent = stats
        clicks2.Parent = stats
        clicks3.Parent = stats
        clicks4.Parent = stats
        clicks5.Parent = stats
        clicks6.Parent = stats
        clicks1337.Parent = stats
        stats.Parent = newPlayer

clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end) 
clicks.Changed:connect(function() onLevelUp(newPlayer, clicks2, clicks) end) 

        newPlayer:WaitForDataReady() -- IMPORTANT: after player.DataReady = true, it won't become false again. You need DataReady to be true for a player before you can load or save data.

        -- try loading the player's score
        loadScore(newPlayer, clicks)
        loadScore2(newPlayer, clicks2)
        loadScore3(newPlayer, clicks3)
        loadScore4(newPlayer, clicks4)
        loadScore5(newPlayer, clicks5)
        loadScore6(newPlayer, clicks6)


newPlayer.CharacterAdded:connect(function(c) 
    c.Humanoid.MaxHealth = newPlayer.Levelss.Level.Value * 10 + 100
    c.Humanoid.Health = newPlayer.Levelss.Level.Value * 10 + 100 
    end) 
end 

function onPlayerRemoving(player)
    print("Attempting to save score for " .. player.Name)
    local stats = player:FindFirstChild("Levelss")
    if (stats ~= nil) then 
        local clicks = stats:FindFirstChild("Level")
        local clicks2 = stats:FindFirstChild("XP")
        local clicks3 = stats:FindFirstChild("FAP")
        local clicks4 = stats:FindFirstChild("RandomMove")
        local clicks5 = stats:FindFirstChild("Muny")
        local clicks6 = stats:FindFirstChild("RangedPunch")
        if (clicks ~= nil)and(clicks2 ~= nil)and(clicks3 ~= nil)and(clicks4 ~=nil)and(clicks5 ~=nil) then
            saveScore(player, clicks.Value)
            saveScore2(player, clicks2.Value)
            saveScore3(player, clicks3.Value)
            saveScore4(player, clicks4.Value)
            saveScore5(player, clicks5.Value)
            saveScore6(player, clicks6.Value)
        end
    end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerRemoving)

Data Storage:

local DSService = game:GetService('DataStoreService'):GetDataStore('soutpansaDataStoreForPunch')
ID = 69 --If you change this then it will reset everyones stats
SaveTime = 60 --Shouldn't be less then 60
leaderboardname = "Levelss" --The name of the leader board

StatList = {Muny = 250 , Level = 1 , XP = 0 , FAP = 1 , RandomMove = 0 , RangedPunch = 0 }
--StartingMoney would give new players the stat StartingMoney with the value 5
--Put any of your values here
--Values are loaded when the player joins and saved when they exit
--Values are also saved every SaveTime seconds

----- You shouldn't need to change anything below this line -----

function CreateValue(Name, Parent, Value) --If value is set to a number then new players will start with that amount.
    Value = Value or 0
    local savevalue =  Instance.new('IntValue', Parent)
    savevalue.Name = Name
    savevalue.Value = Value
    return savevalue
end

function GetValue(uniquekey)
    -- GetAsync
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        return GetSaved
    end
end

game.Players.PlayerAdded:connect(function(plr)
    local uniquekey = ID..plr.userId
    local Levelss = Instance.new('IntValue', plr)
    Levelss.Name = leaderboardname
    for i,v in pairs(StatList) do
        local Stat = GetValue(i..uniquekey)
        if Stat then
            CreateValue(i, Levelss, Stat) --Old player load their value
        else
            CreateValue(i, Levelss, v) --New player load the default value
        end
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = ID..plr.userId
    if plr:FindFirstChild(leaderboardname) then
        for i,v in pairs(StatList) do
            if plr[leaderboardname]:FindFirstChild(i) then
                DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
            end
        end
    end
end)

while wait(SaveTime) do
    P = game:GetService("Players"):GetChildren()
    for o=1, #P do
        if P[o] then
            local uniquekey = ID..P[o].userId
            if P[o]:FindFirstChild(leaderboardname) then
                for i,v in pairs(StatList) do
                    if P[o][leaderboardname]:FindFirstChild(i) then
                        DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
                    end
                end
            end
        end
    end
end



0
You will quickly max out the request limits using this method as you save and get each value individually. User#5423 17 — 7y
0
Then how would you do it? .-. soutpansa 120 — 7y
0
Use JSON AlphaGamer150 101 — 7y

Locked by Goulstem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?