Broken data script

So yeah the title sums it up

It says “All good” then prints the data then does nothing after that :frowning:

Scripts (very messy)

Server script
--Variables
remote = game.ReplicatedStorage.SaveAvatar
dss = game:GetService("DataStoreService")
avatarStore = dss:GetDataStore("avatarStore")
ts = game:GetService("TeleportService")
players = game:GetService("Players")
debounce = true

--Functions


task.wait(3)
debounce = false

function isDataThenTele(player)
local success,errorMessage = pcall(function()
	local data1 = avatarStore:GetAsync("Shirt_"..player.UserId)
	local data2 =	avatarStore:GetAsync("Pants_"..player.UserId)
	local data3 =	avatarStore:GetAsync("Face_"..player.UserId)
	local data4 = avatarStore:GetAsync("Color_"..player.UserId)
	if (data1 and data2) and (data3 and data4) then
		print("All good!")
			print(data1)
				print(data2)
			print(data3)
		print(data4)
		return true
	else
		print("One of these is missing..")
			print(data1 ~= nil)
				print(data2 ~= nil)
			print(data3 ~= nil)
		print(data4 ~= nil)
			return false
		end
	end)
	if not success then
		warn(errorMessage)
		player:Kick("There was an error fetching data.. :(")
	end
end




function TeleportAndSave(player,Clothes)
	local button = player.PlayerGui.CharacterCustomization.Frame.Confirm
	local shirtkey = "Shirt_"..player.UserId
	local pantskey = "Pants_"..player.UserId
	local facekey = "Face_"..player.UserId
	local colorkey = "Color_"..player.UserId
	if debounce == false then
		button.Text = ("Loading..")
			debounce = true
		local success, errorMessage = pcall(function()
			avatarStore:SetAsync(shirtkey,Clothes[1])
			avatarStore:SetAsync(pantskey,Clothes[2])
			avatarStore:SetAsync(colorkey,Clothes[3])
			avatarStore:SetAsync(facekey,Clothes[4])
			debounce = false
			task.wait(2)
			button.Text = "Teleporting.. :)"
			local hasData = isDataThenTele(player)
			if hasData	then
				print("Has data, next stage")
				local success, errorMessage = pcall(function()
					local TeleOptions = Instance.new("TeleportOptions")
					local TeleOptionsSet = {
						data1 = avatarStore:GetAsync("Shirt_"..player.UserId),
						data2 =	avatarStore:GetAsync("Pants_"..player.UserId),
						data3 =	avatarStore:GetAsync("Face_"..player.UserId),
						data4 = avatarStore:GetAsync("Color_"..player.UserId)
					}	
					print(TeleOptions)
					TeleOptions:SetTeleportData(TeleOptionsSet)
					if not game:GetService("RunService"):IsStudio() then
						ts:TeleportAsync(7279402264,{player},TeleOptions)
					else
						print("In studio..")
					end
				end)
				if not success then
					print(errorMessage)
					player:Kick("Sorry, teleport failed :( "..errorMessage)
				end
			elseif hasData == false then
				print("No data...")
			end	
		end)
		if not success then
			print(errorMessage)
			button.Text = ("Sorry, an error occured, try again. :(")
			debounce = false
		end
	elseif debounce == true then
		warn("Debounce!")
	end
end

players.PlayerAdded:Connect(function(plr)
	print(plr)
	if isDataThenTele(plr) then
		local success, errorMessage = pcall(function()
			local TeleOptions = Instance.new("TeleportOptions")
			local TeleOptionsSet = {
				data1 = avatarStore:GetAsync("Shirt_"..plr.UserId),
				data2 =	avatarStore:GetAsync("Pants_"..plr.UserId),
				data3 =	avatarStore:GetAsync("Face_"..plr.UserId),
				data4 = avatarStore:GetAsync("Color_"..plr.UserId)
			}	
			print(TeleOptions)
			TeleOptions:SetTeleportData(TeleOptionsSet)
			if not game:GetService("RunService"):IsStudio() then
				ts:TeleportAsync(7279402264,{plr},TeleOptions)
			else
				print("In studio..")
			end
		end)
		if not success then
			print(errorMessage)
			plr:Kick("Sorry, teleport failed :( "..errorMessage)
		end
	else
		TeleportAndSave()
	end
end)

remote.OnServerEvent:Connect(TeleportAndSave)

Local script
button = script.Parent
sound = button.Ding
saveavatar = game.ReplicatedStorage.SaveAvatar
Character = workspace.CustomizableCharacter

task.wait(1)

button.Activated:Connect(function()
	sound.Playing = true
	local Clothes = {
		Character:FindFirstChildWhichIsA("Shirt").ShirtTemplate,
		Character:FindFirstChildWhichIsA("Pants").PantsTemplate,
		tostring(Character.BodyColors.HeadColor),
		tostring(Character.Head.Face.Texture)
	}
	saveavatar:FireServer(Clothes)
end)

I’d suggest using a datastore module. They basically do all of the data managing for you, you just have to utilize them right. A data module I love is DataStore2 by Kampfkarren, info found here: How to use DataStore2 - Data Store caching and data loss prevention

It would probably work but I don’t really wanna rewrite my whole code.

Still don’t have a solution :(

You should use one pcall function for every GetAsync or SetAsync if you are going to use the variables. Also, as mentioned maybe think about using datastore 2.

2 Likes

I’ve noticed that you gather the four data twice. Wouldn’t it be ideal to have isDataThenTele return them in an array? You could have it return both a boolean and an array, and have it return nil for the array if the returned boolean is false. (It may be ideal to rename the function after that, but that’s less important)

local hasData = isDataThenTele(player) would become
local hasData, TeleOptionsSet = isDataThenTele(player), then you can make changes from there.

Can you give an example? Im not sure how I would incorporate that into the script

Here is a simple example.

function GiveStuff()
    return 1, 2
end
local a, b = GiveStuff()
print(a, b)
print(a + b)

Honestly though, you can just have the function return an array containing the 4 data if all of them are present, or return nil of any of them are not present, and then have TeleportAndSave check whether or not the call to isDataThenTele returns nil. Either way will help.

I got a solution already but tysm

1 Like