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

How would i go about fixing my data store?

Asked by 4 years ago

I am trying to make it when u join you start at level 1 and max exp is 100 but when I start all values = 0 I have tried setting the stat values to this and also tried putting else level.value = 1 etc under the if success part my question is how would I make it that when you play you start at level 1 and max exp 100 here is the code for my data store

local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats"

local Level = Instance.new("IntValue", leaderstats)
Level.Name = "Level"


local exp = Instance.new("IntValue", leaderstats)
exp.Name = "Current"


local maxExp = Instance.new("IntValue", leaderstats)
maxExp.Name = "Max"

local Strength =Instance.new("IntValue",leaderstats)
Strength.Name = "Strength"
Strength.Value = 0

--level change at max exp--
exp.Changed:Connect(function(val)
    if exp.Value >= maxExp.Value then
    Level.Value = Level.Value + 1
    exp.Value = exp.Value - maxExp.Value 
        maxExp.Value = maxExp.Value * 1.25
        Strength.Value = Strength.Value + 5
    end
end)

local levelData
local xpData
local maxData

    local success,errormessage = pcall (function()  
    levelData = playerData:GetAsync(player.UserId..("Level"))
    xpData = playerData:GetAsync(player.UserId..("Current"))
    maxData = playerData:GetAsync(player.UserId..("Max"))

    end)

    if success then
    Level.Value = levelData
    exp.Value = xpData
    maxExp.Value = maxData
else
        warn("there was an loading error".. errormessage)
end

game.Players.PlayerRemoving:Connect(function(player)
    local success,errormessage = pcall (function()
    playerData:SetAsync(player.UserId.."Level",player.leaderstats.Level.Value)
    playerData:SetAsync(player.UserId.."Current",player.leaderstats.Current.Value)
    playerData:SetAsync(player.UserId.."Max",player.leaderstats.Max.Value)

end)
        if success then 
        print("Data Has been saved successfuly")
    else
        warn("there was an error saving")
        end

end)

end)

1 answer

Log in to vote
0
Answered by 4 years ago
game.Players.PlayerRemoving:Connect(function(player)
       local success,errormessage = pcall (function()
        local lead = player:WaitForChild("leaderstats")
        playerData:SetAsync(player.UserId.."Level",lead.Level.Value)
        playerData:SetAsync(player.UserId.."Current",lead.Current.Value)
        playerData:SetAsync(player.UserId.."Max",lead.Max.Value)
    end)
end)

I don't know why, but something like this ^ normally works for me. You might need to use WaitForChild() for all three lines. I hope this helps but if you want an exact error message, join your game with a friend and see what happens when the friend joins and leaves in the dev console.

Additionally, if you are testing in studio, make sure studio has access to api services (in game settings under security)

0
sorry I might have not said as clear as I should have but what I meant is I know that the dataStore is working but for some reason when i start the game my value for my level is 0 and my max xp is at 0 so I am trying to make it when u first join the game you start at Level 1 and have Max Xp at 100 idk why I cant figure this out and it is probably a Easy fix. Danieldevlin67 2 — 4y
0
O Heavenlyblobmaster 271 — 4y
0
O Heavenlyblobmaster 271 — 4y
0
try this: if THING.value == 0 then THING.value = NUMBER end at the end of the playerAdded function Heavenlyblobmaster 271 — 4y
0
that didnt work but i tried it in a different place and it works now thanks! Danieldevlin67 2 — 4y
Ad

Answer this question