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

Why is the script I wrote for saving the level of the players not working?

Asked by 4 years ago

So this is my entire script:

Script Number 1 (Server)

PS = game:GetService("Players")
DS = game:GetService("DataStoreService")

local LevelDataStore = DS:GetDataStore("LevelStorage")
local GuiDataStore = DS:GetDataStore("GuiLevelStorage")

PS.PlayerAdded:Connect(function(playername)
-- Adds the "PlayerFolder" in each player who joins.
    local playerfolder = Instance.new("Folder", playername)
    playerfolder.Name = "PlayerFolder"
-- Adds a level IntValue to the "PlayerFolder"  
    local level = Instance.new("IntValue", playername:WaitForChild("PlayerFolder"))
    level.Name = "Level"
-- When a player joins the game it changes their level Screen Gui to say "Level:(their level)"
    playername.PlayerGui:WaitForChild("LevelGui").TextLabel.Text = "Level: "..playername.PlayerFolder.Level.Value

    local Data
    local success, errormessage = pcall(function()
        local Data = LevelDataStore:GetAsync(playername.UserId)
    end)
    if success then
        playername.PlayerFolder.Level.Value = Data
        print("Successfully loaded the data")
    else
        warn(errormessage)
        print("There was an error while retrieving the data.")
    end
end)


PS.PlayerRemoving:Connect(function(playername)
    local success, errormessage = pcall(function()
        LevelDataStore:SetAsync(playername.UserId, playername.PlayerFolder.Level.Value)
    end)
    if success then
        print("Data was successfully saved.")
    else
        warn(errormessage)
    end
end)

Script Number 2 (Local)

PS = game:GetService("Players")

player = game.Players.LocalPlayer
Gui = script.Parent
level = player.PlayerFolder.Level

level.Changed:Connect(function()
    Gui.TextLabel.Text = "Level: "..level.Value
end)

So my goal with this script was to make it so when a player joins they get a gui displaying their level and every time the level IntValue is changed it updates to what it currently is. So the gui works fine is the overall saving of the data and I am confused on why it is not working.

Short and simple version: It is not saving my data and both of the pcalls are not detecting errors.

Let me know if you need anymore information. Thanks.

Answer this question