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

What did I do wrong with the data saving?

Asked by 5 years ago

So I'm trying to get used to Data Storage, and I made this script that is supposed to store the sitKeybind.Value. It prints the "success1" and the "success2", but it doesn't actually save the value. What did I do wrong?

This is a ServerScript in the ServerScriptService.

DSS = game:GetService("DataStoreService")
keybindStorage = DSS:GetDataStore("keybindStorage")

sitKeybind = game:GetService("ReplicatedStorage").Values.keybindValue_Sit


game:GetService("Players").PlayerAdded:Connect(function(player)

    local data
    local success1, errormess1 = pcall(function()
        data = keybindStorage:GetAsync(player.UserId)
    end)

        if success1 then
            data = sitKeybind.Value
            print("Data retrieved for " ..player.UserId ..data)
        else
            print ("Failed to retrieve the data for " ..player.UserId)
        end

end)    

game:GetService("Players").PlayerRemoving:Connect(function(player)

    local success2, errormess2 =  pcall(function()  

    keybindStorage:SetAsync(player.UserId, sitKeybind.Value)

    end)

        if success2 then
            print("Successfully saved for " ..player.UserId .. "  Value:" ..sitKeybind.Value)
        else
            print("Data save failed for " ..player.UserId)
        end

end)


1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Here is my server data store. maybe u can peek on that. As i see u did nothing wrong.

local TimeToWait = 60*10
local Time = 30
--  Set DataStoreName find in game as StringValue.
local DataStoreName = game.ReplicatedStorage.Values.ServerName.Value

--  Get service
local ServerDataStore = game:GetService("DataStoreService")
--  Get the data from DataStoreName
local ServerData = ServerDataStore:GetDataStore(DataStoreName)
--  Set Values
local GetDataStore
--  Make Server Parts
local Server = {}
--  Set ServerId in Server from above, find in game as NumberValue
    Server.ServerId = game.ReplicatedStorage.Values.ServerId.Value
--  Set ServerName to DataStoreName
    Server.Name = DataStoreName
--  Set my SurfaceGuiScreen on a door for my store like open or close
--  Find it in Game as boolValue
    Server.Store = game.Workspace.Store.SurfaceGui.State
--  Set StoreValue for Open or Closed (true or false)
    Server.StoreState = game.Workspace.Store.StoreState
--  Set my SurfaceGuiScreen on a door for my Bank like open or close
--  Find it in Game as boolValue
    Server.Bank = game.Workspace.Bank.Counter.CounterDoor.SurfaceGui.State
--  Set BankValue for Open or Closed (true or false)
    Server.BankState = game.Workspace.Bank.Counter.CounterDoor.Bank.BankState
    Server.Restart = game.ReplicatedStorage.Values.Restart
    Server.Restart1 = game.ReplicatedStorage.Values.Restart1
    Server.Helper = game.ReplicatedStorage.Values.Helper
    Server.spelercount = game.ReplicatedStorage.Values.SpelersCount


--  Make 2 ID's for Store and Bank
local ServerStoreData = {Server.ServerId.."-"..Server.StoreState.Name}
local ServerBankData = {Server.ServerId.."-"..Server.BankState.Name}
local ServerRestartData = (Server.ServerId.."-"..Server.Restart.Name)
local ServerRestart1Data = (Server.ServerId.."-"..Server.Restart1.Name)
local HelperData = (Server.ServerId.."-"..Server.Helper.Name)
local spelercountdata = (Server.ServerId.."-"..Server.spelercount.Name)

--  A default value
local Default = false

--  Function check open or closed by ID and What is Server.BankState or Server.StoreState
local function CheckState(ServerId,What)

--  Get the data from the givin ID
    GetDataStore = ServerData:GetAsync(ServerId)
--  If there is data and it true or false then do
    if GetDataStore then

--      Make Text to print
        local Text = "Server: "..DataStoreName.. " loading..."
--      put in output screen from server
        print(Text)
        print(What.Name.." loading...")
        What.Value = GetDataStore
        print(What.Name.." loaded")
        local Text = "Server: "..DataStoreName.. " loaded"
        print(Text)

--      look if name's are the same and value is true 
        if What.Name == Server.BankState.Name and What.Value == true then
--          set text from SurfaceGuiScreen on door open
            Server.Bank.Text = "Open"
--          set color green
            Server.Bank.BackgroundColor3 = Color3.new(0, 255, 0)
--          show in output from server
            print("Bank Open")

        end

        if What.Name == Server.StoreState.Name and What.Value == true then

            Server.Store.Text = "Open"
            Server.Store.BackgroundColor3 = Color3.new(0, 255, 0)
            print("Store Open")

        end

        if What.Name == Server.Restart1.Name and What.Value == true then

            Server.Restart.Value = false
            Server.Restart1.Value = false
            ServerData:SetAsync(ServerRestartData,Server.Restart.Value)
            ServerData:SetAsync(ServerRestart1Data,Server.Restart1.Value)

        end

        if What.Name == Server.Restart.Name and What.Value == true then

            Server.Restart1.Value = true
            ServerData:SetAsync(ServerRestart1Data,Server.Restart1.Value)

            local tijd = 30
            local text = "Server Restarts in "..tijd

            for i, player in pairs(game.Players:GetPlayers()) do

                local restart = Instance.new("ScreenGui",player.PlayerGui)
                restart.Name = "Restart"
                restart.Parent = player.PlayerGui

                local Text = Instance.new("TextLabel",restart)
                Text.Name = "Text"
                Text.Parent = restart
                Text.Position = UDim2.new(0.20, 0, 0.15, 0)
                Text.Size = UDim2.new(0, 500, 0, 500)
                Text.BackgroundColor3 = BrickColor.Red().Color
                Text.BackgroundTransparency = UDim2.new(0)
                Text.BorderSizePixel = UDim2.new(2)
                Text.Font = "Arial"
                Text.Text = text
                Text.TextScaled = true

                local Script = game.ReplicatedStorage.Scripts.pr
                local place = restart

                local copy = Script:Clone()
                copy.Parent = place


            end


        end

--      look if name's are the same and value is false
        if What.Name == Server.BankState.Name and What.Value == false then
--          set text from SurfaceGuiScreen on door closed
            Server.Bank.Text = "Closed!"
--          set color red
            Server.Bank.BackgroundColor3 = Color3.new(255, 0, 0)
--          show in output from server
            print("Bank Closed")

        end

        if What.Name == Server.StoreState.Name and What.Value == false then

            Server.Store.Text = "Closed!"
            Server.Store.BackgroundColor3 = Color3.new(255, 0, 0)
            print("Store Closed")

        end


--  On no Data in GetDataStore do this
    else

        if What.Value ~= nil then
            Default = What.Value
        end
--      Save Special ID with Default Value (= false)
        ServerData:SetAsync(ServerId,Default)
--      Show in output screen in server
        print("server Default")
        local Text = "Server: "..DataStoreName.. " Setting..."
--      put in output screen from server
        print(Text)
        print(What.Name)
        print(What.Value)
        print(GetDataStore)
        local Text = "Server: "..DataStoreName.. " Done"
--      put in output screen from server
        print(Text)

--      look if name's are the same and value is false
        if What.Name == Server.BankState.Name and What.Value == false then

            Server.Bank.Text = "Closed!"
            Server.Bank.BackgroundColor3 = Color3.new(255, 0, 0)
            print("Store Closed")

        end

        if What.Name == Server.StoreState.Name and What.Value == false then

            Server.Store.Text = "Closed!"
            Server.Store.BackgroundColor3 = Color3.new(255, 0, 0)
            print("Store Closed")

        end
    end
end

--Make a loop
while true do

    CheckState(spelercountdata, Server.spelercount)
    wait(Time)
    CheckState(ServerBankData,Server.BankState)
    wait(Time)
    --  Funtion(Special ID, What {Store or Bank in my case})
    CheckState(ServerStoreData,Server.StoreState)
    wait(Time)
    CheckState(HelperData, Server.Helper)
    wait(Time)
    CheckState(ServerRestart1Data,Server.Restart1)
    wait(Time)
    CheckState(ServerRestartData,Server.Restart)


    wait(TimeToWait)

end

Happy scripting!

oh yeah for got this one out my otther script

ServerData:SetAsync(ServerId,What.Value)
0
last one i use and onupdate function like on the first tcript funtion chackstate User#27824 0 — 5y
Ad

Answer this question