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

How do I integrate this function into this code?

Asked by
soutpansa 120
7 years ago

My game used to use Data Persistence, and it also had a level up function inside of the data persistence script. Now that I dont use data persistence, instead using data storage, the level up code wont work. Here is the level up 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)

And here is that Data Storage code:

local DSService = game:GetService('DataStoreService'):GetDataStore('Statz')
ID = 0 --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

And this is how I combined them:

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


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)

I was thinking that maybe the level up code had to be before all of the ends at the last part of the Data Storage, but I'm not sure. Any ideas?

0
You have provided a whole lot of code that isn't needed to answer your question. DataStores should only require a PlayerAdded event, and a PlayerRemoving event. Please narrow this down for us. Goulstem 8144 — 7y
0
Please properly indent your code before posting it. Async_io 908 — 7y
0
The first script that is Async_io 908 — 7y
0
Sorry about that. I'm going to make a new post with indented code soutpansa 120 — 7y

Answer this question