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

How do I save player's data?

Asked by 8 years ago

Hello, so basically i'am trying to make this script save my player's stats when they logoff and load then when they login. The script is just not working, no idea why, was reading the article "Saving Player Data" in roblox wiki but my current knowledge of lua isn't good enough to let me adjust that script there to the one i have... No idea what the problem is but here is the script:

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")
local loadedstats = false
function PlayerEntered(player)
    repeat wait() until player.Character
        repeat wait() until player.Character.Stats
            local stats = player.Character:WaitForChild("Stats")
        local goldnew = Instance.new("IntValue")
        goldnew.Parent = player.Character.Stats
        goldnew.Value = "0"
        goldnew.Name = "Gold"
        local levelnew = Instance.new("IntValue")
        levelnew.Name = "Level"
        levelnew.Parent = player.Character.Stats
        levelnew.Value = "1"
        local xpnew = Instance.new("IntValue")
        xpnew.Name = "Xp"
        xpnew.Parent = player.Character.Stats
        xpnew.Value = "0"
        local constnew = Instance.new("IntValue")
        constnew.Name = "Constitution"
        constnew.Parent = player.Character.Stats
        constnew.Value = ""
        local xptlvnew = Instance.new("IntValue")
        xptlvnew.Name = "Xptlv"
        xptlvnew.Parent = player.Character.Stats
        xptlvnew.Value = "70"
        local skillpnew = Instance.new("IntValue")
        skillpnew.Name = "SkillPoints"
        skillpnew.Parent = player.Character.Stats
        skillpnew.Value = "0"
        local hp = player.Character.Humanoid:WaitForChild("Health")
        local maxhp = player.Character.Humanoid:WaitForChild("MaxHealth")
        local gold = stats:WaitForChild("Gold")
        local level = stats:WaitForChild("Level")
        local xp = stats:WaitForChild("Xp")
        local const = stats:WaitForChild("Constitution")
        local xptlv = stats:WaitForChild("Xptlv")
        local skillp = stats:WaitForChild("SkillPoints")        


        wait(5)
        if CashStore:GetAsync("Gold_"..player.Name) ~= nil then
            gold.Value = CashStore:GetAsync("Gold_"..player.Name)
        else
            gold.Value = 0
        end
        if CashStore:GetAsync("Level_"..player.Name) ~= nil then
            level.Value = CashStore:GetAsync("Level_"..player.Name)
        else
            level.Value = 0
        end
        if CashStore:GetAsync("Xp_"..player.Name) ~= nil then
            xp.Value = CashStore:GetAsync("Xp_"..player.Name)
        else
            xp.Value = 0
        end
        if CashStore:GetAsync("Const_"..player.Name) ~= nil then
            const.Value = CashStore:GetAsync("Const_"..player.Name)
        else
            const.Value = 0
        end
        if CashStore:GetAsync("Xptlv_"..player.Name) ~= nil then
            xptlv.Value = CashStore:GetAsync("Xptlv_"..player.Name)
        else
            xptlv.Value = 70
        end
        if CashStore:GetAsync("SkillP_"..player.Name) ~= nil then
            skillp.Value = CashStore:GetAsync("SkillP_"..player.Name)
        else
            skillp.Value = 0
        end
        if CashStore:GetAsync("Health_"..player.Name) ~= nil then
            hp.Value = CashStore:GetAsync("Health_"..player.Name)
        else
            hp.Value = 100
        end 
        if CashStore:GetAsync("MaxHealth_"..player.Name) ~= nil then
            maxhp.Value = CashStore:GetAsync("MaxHealth_"..player.Name)
        else
            maxhp.Value = 100
        end 
        loadedstats = true
        player.Character.IsLoading.Value = false
end

function PlayerLeaving(player)
    if loadedstats == true then 
    local stats = player.Character:WaitForChild("Stats")    
    local hp = player.Character.Humanoid:WaitForChild("Health")
    local maxhp = player.Character.Humanoid:WaitForChild("MaxHealth")
    local gold = stats:WaitForChild("Gold")
    local level = stats:WaitForChild("Level")
    local xp = stats:WaitForChild("Xp")
    local const = stats:WaitForChild("Constitution")
    local xptlv = stats:WaitForChild("Xptlv")
    local skillp = stats:WaitForChild("SkillPoints")
            CashStore:SetAsync("Gold_"..player.Name, gold.Value)
            CashStore:SetAsync("Level_"..player.Name, level.Value)
            CashStore:SetAsync("Xp_"..player.Name, xp.Value)
            CashStore:SetAsync("Const_"..player.Name, const.Value)
            CashStore:SetAsync("Xptlv_"..player.Name, xptlv.Value)
            CashStore:SetAsync("SkillP_"..player.Name, skillp.Value)
            CashStore:SetAsync("Health_"..player.Name, hp)
            CashStore:SetAsync("MaxHealth_"..player.Name, maxhp)
    end 
end 

game.Players.PlayerAdded:connect(PlayerEntered)
game.Players.PlayerRemoving:connect(PlayerLeaving)

Feel free to ask anything if there is something unclear and thank you in advance.

0
What type of script is it? What does the output say? fahmisack123 385 — 8y
0
Sorry i havent answered. Was busy. A normal script. It doesnt say anything, no errors. User#11680 0 — 8y
0
Ok so tried it on local script and it says "Attempt to connect failed: Passed value is not a function (Line 98)". (Still not changed the way LightModed offered to) User#11680 0 — 8y
0
Just realised there are more problems happening, ill try to fix those and if i'll fail or succeed ill let you know User#11680 0 — 8y
0
Ok so i modified the script. Now I realised that the PlayerEntered function's part after adding the new IntValues doesnt work.(Made it so the loading screen ends after loading data and the loading screen never ends.) Any ideas? The output doesnt show any errors User#11680 0 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

Instead of making it in a whole script, I suggest using PlayerRemoved event.

0
Ok, ill try that in a hour or so and tell you if that works. User#11680 0 — 8y
0
Ok so i modified the script. Now I realised that the PlayerEntered function's part after adding the new IntValues doesnt work.(Made it so the loading screen ends after loading data and the loading screen never ends.) Any ideas? The output doesnt show any errors User#11680 0 — 8y
Ad

Answer this question