for X,G in pairs(workspace.QuestTriggers:GetChildren())do local A=replicatedStorage.Quests:FindFirstChild(G.Name)if A then G.Touched:Connect(function(X)local G=players:GetPlayerFromCharacter(X.Parent)if G and(not script:FindFirstChild(G.Name.."\068\101\099\105\100\105\110\103")and(not G.Quests:FindFirstChild(A.Name)and not G.CompletedQuests:FindFirstChild(A.Name)))then local X=Instance.new("\077\111\100\101\108")X.Name=G.Name.."\068\101\099\105\100\105\110\103"X.Parent=script local l pcall(function()l=replicatedStorage.ClientRemotes.OfferQuest:InvokeClient(G,A)end)if l then local X=(require(questSystem)).GiveQuest(G,A.Name);(require(questSystem[X.QuestType.Value])).CheckIfCompleted(G,X)end wait(2)X:Destroy()end end)else warn("\067\111\117\108\100 \110\111\116 \102\105\110\100 \097 \109\097\116\099\104\105\110\103 \113\117\101\115\116 \105\110 \082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101\046\081\117\101\115\116\115 \102\111\114 \116\104\101 \116\114\105\103\103\101\114 \110\097\109\101\100 \""..(G.Name.."\""))end end for X,G in pairs(replicatedStorage.Quests:GetChildren())do if G.QuestType.Value=="\084\111\117\099\104\079\114\098"then local X=G.Requirements:FindFirstChildOfClass("\079\098\106\101\099\116\086\097\108\117\101")if X and X.Value then X.Value.Touched:Connect(function(X)local A=players:GetPlayerFromCharacter(X.Parent)if A then local X=A.Quests:FindFirstChild(G.Name)if X then(require(questSystem.TouchOrb)).CheckIfCompleted(A,X)end end end)end end end

for _,trigger in pairs(workspace.QuestTriggers:GetChildren()) do
	local quest = replicatedStorage.Quests:FindFirstChild(trigger.Name)
	if quest then
		trigger.Touched:Connect(function(part)
			local player = players:GetPlayerFromCharacter(part.Parent)
			if player and not script:FindFirstChild(player.Name .. "Deciding") and not player.Quests:FindFirstChild(quest.Name) and not player.CompletedQuests:FindFirstChild(quest.Name) then
				local tag = Instance.new("Model")
				tag.Name = player.Name .. "Deciding"
				tag.Parent = script
				local acceptedQuest
				pcall(function() -- Player might leave during the invoke, causing the tag to stay forever
					acceptedQuest = replicatedStorage.ClientRemotes.OfferQuest:InvokeClient(player, quest)
				end)
				if acceptedQuest then
					local quest = require(questSystem).GiveQuest(player, quest.Name)
					require(questSystem[quest.QuestType.Value]).CheckIfCompleted(player, quest) -- Player may have already met the completion requirements
				end
				wait(2)
				tag:Destroy()
			end
		end)
	else
		warn('Could not find a matching quest in ReplicatedStorage.Quests for the trigger named "' .. trigger.Name .. '"')
	end
end
for _,quest in pairs(replicatedStorage.Quests:GetChildren()) do
	if quest.QuestType.Value == "TouchOrb" then -- Make quest touch connections
		local destinationOrb = quest.Requirements:FindFirstChildOfClass("ObjectValue")
		if destinationOrb and destinationOrb.Value then
			destinationOrb.Value.Touched:Connect(function(part)
				local player = players:GetPlayerFromCharacter(part.Parent)
				if player then
					local matchingQuest = player.Quests:FindFirstChild(quest.Name)
					if matchingQuest then
						require(questSystem.TouchOrb).CheckIfCompleted(player, matchingQuest)
					end
				end
			end)
		end
	end
end
