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

Gui button to save values in data store not working?

Asked by 4 years ago

So ive gotten it to save when you click a block so then I tried it with a gui button and it seems to not even run the full script. Much help appreciated.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

script.Parent.MouseButton1Click:Connect(function(click)
    game.Players.PlayerRemoving:Connect(function(player)
        local success, errormessage = pcall(function()
            myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
        end)
        if success then
            print("Saved!!")
        else
            print("Something is not right!!!")
            warn(errormessage)
        end
    end)

1 answer

Log in to vote
0
Answered by 4 years ago

First thing, DataStore Service is only for ServerScripts and LocalScripts. Also, there is no use for using Gui Buttons for saving Data. You can simple create a ServerScript, put it in ServerScriptService. Code : [The one from your code]

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerRemoving:Connect(function(player)
        local success, errormessage = pcall(function()
            myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
        end)
        if success then
            print("Saved!!")
        else
            print("Something is not right!!!")
            warn(errormessage)
        end
end)

Lemme know if it helps!

Ad

Answer this question