Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Gui button to save values in data store not working?

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

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.