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

How to put this function into my data storage?

Asked by
soutpansa 120
7 years ago

My game used to use data persistence, but now that I switched to Data storage, the level up function wont work. How do I put these two together?

Data Storage:

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

Level up:

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)
    clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end) --Clicks 2 is XP, and clicks is Level
    clicks.Changed:connect(function() onLevelUp(newPlayer, clicks2, clicks) end) 

    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)
2
You cannot save a function in a data store, can you also explain the problem in more detail and if there are any errors. User#5423 17 — 7y
0
You're supplying a lot of un-needed code. DataStore scripts should essentially consist of a few declarations, a playeradded event, and a playerremoving event. Maybe a game.OnClose if ur a cool kid. Goulstem 8144 — 7y

Answer this question