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

DataStore script won't save GUI Buttons [STILL UNANSWERED]?

Asked by
Vid_eo 126
5 years ago
Edited 5 years ago

I'm trying to make a DataStore script that saves and loads GUI buttons and money; it saves/loads money just fine, but it doesn't save/load the GUI buttons; I think there's a problem with the loading.

SCRIPT:

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("danceSave")
local ds2 = DataStoreService:GetDataStore("MoneySave")

game:GetService("Players").PlayerAdded:Connect(function(Player) --When the player joins the game, load value.

    local Stats = Instance.new('Folder')
    Stats.Name = "leaderstats"

    local Money = Instance.new('NumberValue', Stats)
    Money.Name = "Money"

    Stats.Parent = Player


    --load data
    local moneyData;
    local dancesData;

    local CanSave = Instance.new('BoolValue', Player)

    CanSave.Name = "CanSaveData"
    CanSave.Value = true

    local DataFetchSuccess, ErrorMessage = pcall(function()
        moneyData = ds2:GetAsync(tostring(Player.UserId))
        dancesData = ds:GetAsync(tostring(Player.UserId))
    end)
    if DataFetchSuccess then 
        if moneyData ~= nil then --This could be where the problem is
            print("Money save found")
            Money.Value = moneyData
        else
        print("No money save found.")
            Money.Value = 0 -- Where money value is changed to 0
        end
        --print(Money.Value)
        print("k")
        if dancesData ~= nil then
            print("k")
            for i,v in pairs(dancesData) do
                print(v)
                local Dance = Player.PlayerGui.Dances:FindFirstChild(v)
                if dancesData then
                    Dance:Clone().Parent = Player.PlayerGui.dancesGui.dancesFrame.danceFrame.danceScrolling
                end         
            end
         print("k")
        end
    else
        Player.CanSaveData.Value = false
        Player:Kick("Your data failed to load! Please rejoin.")
    end
end)

game:GetService("Players").PlayerRemoving:Connect(function(Player) -- When the player leaves the game, data must save.
    if Player.CanSaveData.Value == false then return end --There could also be a problem with saving the money

    local moneyData = Player.leaderstats.Money.Value
    local dancesToSave = {}

    print(moneyData)
    print(dancesToSave)

    for i,v in pairs (Player.PlayerGui.dancesGui.dancesFrame.danceFrame.danceScrolling:GetChildren()) do
    print("loop de loop")
        if v:IsA("TextButton") then
            table.insert(dancesToSave, v.Name)
            print(v.Name)
        end
    end

    local DataWriteSuccess, ErrorMessage = pcall(function()
        print("Saving "..Player.Name.."s stats")
        ds:SetAsync(tostring(Player.UserId), dancesToSave)
        print("ok0")
        ds2:SetAsync(tostring(Player.UserId), moneyData)
        print("ok")
    end)

    if not DataWriteSuccess then
        local Retry_Count = 0

        while Retry_Count < 6 do
            wait(60)
            local Succeed, Error = pcall(function()
        print("Attempting to save "..Player.Name.."'s stats.")
                ds:SetAsync(tostring(Player.UserId), dancesToSave)
                ds2:SetAsync(tostring(Player.UserId), moneyData)
            end)
            if Succeed then print("Saved "..Player.Name.."'s stats") break end
            Retry_Count = Retry_Count + 1
        end
    end
end)


There are no errors.

0
Fun fact: Servers can't/don't see whats in the PlayerGui, Backpack, etc. They really only acknowledge their existence. Async_io 908 — 5y
0
So how would I use RemoteEvents to get around that? Vid_eo 126 — 5y
0
Could I use Global Variables to save the table and then add contents to the table in the LocalScript and then save the table in the serverscript? Vid_eo 126 — 5y
0
nvm, can't use _G. to share between local and server scripts Vid_eo 126 — 5y
0
If _G replicated and you actually used it on the server, this is a whole separate problem. hiimgoodpack 2009 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago

It doesn't save objects. Just save the amount of points that each player has, and then have the GUI display that from the localscript; don't store and clone the objects themselves.

0
I’m trying to index the names of objects into a table and then displaying those objects as TextButtons. The buttons are not related to the money; they’re like animations. Vid_eo 126 — 5y
0
You really shouldn't have the server handle GUI. Fire remote events connected to functions in the player's local scripts that tell the player's GUI what to do. vanilla_wizard 336 — 5y
0
nice LegitimatlyMe 519 — 5y
0
What if I want the LocalScript to refer to a variable located in the DataStore script? Vid_eo 126 — 5y
View all comments (9 more)
0
Have the LocalScript send a get request by firing a remote event connected to the server so it can access the DataStore and return the amount of points to the player vanilla_wizard 336 — 5y
0
I want these GUIs: https://gyazo.com/47f1ee22660f94c4f3005ff407a578ec to move here: https://gyazo.com/fcfe1a78dd8c40233bf979122dcea331. All the variables etc. are correct. The money script works fine. I did this with tools and it also worked well. Vid_eo 126 — 5y
0
I don't think you're approaching this in a very efficient way. Why not have the GUI elements inside of the GUI and have the code that handles the GUI be inside of the GUI? If it's really necessary for the server to handle that, use remote events. Having DataStore save and load the buttons themselves is unnecessary. Save yourself time by handling the UI on the client. vanilla_wizard 336 — 5y
0
How would I check to see if players unlocked the GUIs w.out using a datastore?? Vid_eo 126 — 5y
1
Just use DataStore to save very basic data about the player's achievements (whether a true/false "unlocked" value, an integer "level" or "points", etc). Send that basic data to the client in the GUI over a remote event, and have the GUI handle the GUI from a local script. The local script can have the GUI appear or disappear, but having the server clone it into the player isn't a good idea. vanilla_wizard 336 — 5y
0
Thanks for your help! I thought about it a bit and decided to use values and then use a RemoteEvent to transfer the GUIs if the values were true, and it worked! Vid_eo 126 — 5y
0
I actually still have one more issue. I save the Values in ReplicatedStorage and then replicate those values into the player, but I think that that's breaking the DataStore with multiple people. Should I just put the values inside of the Player? Vid_eo 126 — 5y
0
Never mind, I fixed it! Sorry to bother you Vid_eo 126 — 5y
0
Never mind, it's still broken, but I'll find a way to fix it Vid_eo 126 — 5y
Ad

Answer this question