Will this work? (webhook in a pcall)

i’m unsure if this works. i know my webhook works because i tested it out before. i don’t know if this will work:

local Datastore = game:GetService("DataStoreService")
local https = game:GetService("HttpService")
local url = (webhook url)
local data = Datastore:GetDataStore("data")
player = game.Players.LocalPlayer
local leadstats = Instance.new("Folder")
leadstats.Name = "leaderstats"
leadstats.Parent = player
playerUserId = "Player_"..player.UserId
local checkpoints1 = Instance.new("IntValue")
checkpoints1.Name = "Checkpointch1"
checkpoints1.Parent = leadstats

local temp = Instance.new("IntValue")
temp.Name = "temp"
temp.Parent = leadstats
local chapters = Instance.new("IntValue")
chapters.Name = "chapters"
chapters.Parent = player
game.Players.PlayerAdded:Connect(function(player)

	local save
	local success, errormsg = pcall(function()
		save = data:GetAsync(playerUserId)

	end)
	if success then
		checkpoints1.Value = save
		chapters.Value = save
	else
		game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("DebugScreen"):WaitForChild("DebugFrame").Visible = true
		warn(errormsg)
		local webhookdata = {["embeds"] = {{
			["title"] = "Error",
			["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
		local webhookmsg = https:JSONEncode(webhookdata)
		https:PostAsync(url, webhookmsg)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormsg
	success, errormsg = pcall(function()
		local save

		save = data:SetAsync(playerUserId)
		if success then
			checkpoints1.Value = save
			print("Data saved")
		else
			game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("DebugScreen"):WaitForChild("DebugFrame").Visible = true
			warn(errormsg)
			local webhookdata = {["embeds"] = {{
				["title"] = "Error",
				["description"] = player.Name, "Something went wrong while saving data.", chapters.Value, checkpoints1.Value}}}
			local webhookmsg = https:JSONEncode(webhookdata)
			https:PostAsync(url, webhookmsg)
		end
	end)
end)

i’ve asked this before but no one replied.

set success to false, and test it yourself

game:GetService(“Players”).PlayerRemoving:Connect(function(player)
local success, msg = pcall(function()
return data:SetAsync(player.UserId)
end)
if success then
checkpoints1.Value = save
print(“Data saved”)
else
game.Players.LocalPlayer:WaitForChild(“PlayerGui”):WaitForChild(“DebugScreen”):WaitForChild(“DebugFrame”).Visible = true
warn(msg)
local webhookdata = {[“embeds”] = {{
[“title”] = “Error”,
[“description”] = player.Name, “Something went wrong while saving data.”, chapters.Value, checkpoints1.Value}}}
local webhookmsg = https:JSONEncode(webhookdata)
https:PostAsync(url, webhookmsg)
end
end)

1 Like

so it works? i wasn’t sure because i haven’t used pcalls before.

how do i do that? i really suck when it comes to pcalls.

success = false

if success then

“unknown global sucess” save with my save variable.

“replace that part with”

local Datastore = game:GetService("DataStoreService")
local https = game:GetService("HttpService")
local url = (webhook url)
local data = Datastore:GetDataStore("data")
player = game.Players.LocalPlayer
local leadstats = Instance.new("Folder")
leadstats.Name = "leaderstats"
leadstats.Parent = player
playerUserId = "Player_"..player.UserId
local checkpoints1 = Instance.new("IntValue")
checkpoints1.Name = "Checkpointch1"
checkpoints1.Parent = leadstats

local temp = Instance.new("IntValue")
temp.Name = "temp"
temp.Parent = leadstats
local chapters = Instance.new("IntValue")
chapters.Name = "chapters"
chapters.Parent = player
game.Players.PlayerAdded:Connect(function(player)

	local save
	local success, errormsg = pcall(function()
		save = data:GetAsync(playerUserId)
	end)
	success = false
	if success then
		checkpoints1.Value = save
		chapters.Value = save
	else
		game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("DebugScreen"):WaitForChild("DebugFrame").Visible = true
		warn(errormsg)
		local webhookdata = {["embeds"] = {{
			["title"] = "Error",
			["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
		local webhookmsg = https:JSONEncode(webhookdata)
		https:PostAsync(url, webhookmsg)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormsg
	success, errormsg = pcall(function()
		local save

		save = data:SetAsync(playerUserId)
		success = false
		if success then
			checkpoints1.Value = save
			print("Data saved")
		else
			game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("DebugScreen"):WaitForChild("DebugFrame").Visible = true
			warn(errormsg)
			local webhookdata = {["embeds"] = {{
				["title"] = "Error",
				["description"] = player.Name, "Something went wrong while saving data.", chapters.Value, checkpoints1.Value}}}
			local webhookmsg = https:JSONEncode(webhookdata)
			https:PostAsync(url, webhookmsg)
		end
	end)
end)

if it works, remove success = false

didn’t work. i tested it in studio and left. (trough the pause menu thing)

then the api failed, Did you check the output

yes, i did. no errors from the saving script

local https = game:GetService(“HttpService”)

local url_webhook = “”

local success,err = pcall(function()
local descText = “[!]: Something went wrong while saving %s data [%s, %s]”
local webhookdata = {[“embeds”] = {{
[“title”] = “Error”,
[“description”] = string.format(descText, “PLR NAME”, tostring(math.random(1, 10)), tostring(math.random(1, 10)))}}}
local webhookmsg = https:JSONEncode(webhookdata)
https:PostAsync(url_webhook, webhookmsg)
end)
if (err) then warn(err) end

Make new script and put that code and try also make sure ur url webhook is correct and make sure your game settings>Security>HTTPS API Access is set to allow.

Are you testing in Studio? Maybe try a BindToClose script

What’s that? Sorry i don’t really know how to code that well.

Just return the player removing function using this. It’s good to use for databases as well. Jus read the article I sent to test it out. (When testing in studio sometimes it doesn’t exit out the game correctly)

“The game will wait a maximum of 30 seconds for all bound functions to complete running before shutting down. After 30 seconds, the game will shut down regardless if all bound functions have completed or not.”

1 Like

Thank you, i didn’t understand everything but I’ll research more later.