Lag spikes when using proximity prompts

Above is a video showcasing me using proximity prompts with my Npc’s. Whenever a player uses the prompt, 1 - 3 functions are fired as abilities for player’s items. Particle effects will fly out of the npc, following by a chat message and a animation. When ever I do this there is a huge lag spike. After the 3 seconds the NPC disappears with a smoke particle effect, which also lags

Here is the code that handles the prompt.

if prompt.Name == "GiveJoke" and #jokes >= 1 and #jokes < player.HiddenStats.MaxJokes.Value + 1 then
		local npc = prompt.Parent.Parent
		local animation = npc.Animation
		local npcHumanoid = npc.Humanoid
		local stage = prompt.Parent.Parent.Parent
		local laughAnim = npcHumanoid:LoadAnimation(animation.Laugh)
		
		laughAnim.Priority = Enum.AnimationPriority.Action4
		
		if prompt.Parent.Parent.Parent.Configuration.StageName.Value == "Field" then
			quests.TellJokesAtFarm(game.ReplicatedStorage.Quest, player.Quests.TellJokesAtFarm)
		end
		
		laughAnim:Play()
		
		local chat = chats[math.random(1, #chats)]
		ChatService:Chat(npc.Head, chat, Enum.ChatColor.White)
		prompt.Enabled = false
		
		
		
		

		local coinNoises = npc.HumanoidRootPart.CoinSounds:GetChildren()
		local randomCoinNoise = coinNoises[math.random(1, #coinNoises)]
		randomCoinNoise:Play()

		for i, v in pairs(jokes) do
			
			spawn(function()
				jokeAbility[v.Name]()
				PlayParticalGroup(npc.HumanoidRootPart.LaughEffect, 1, npc.HumanoidRootPart.Position, ReplicatedStorage.Jokes[v.Name].Icon.Value)
			end)
			wait()
		end
		
		local popSound = game.StarterPlayer.Sounds.Pop
		wait(3.3)
		laughAnim:Stop()
		popSound:Play()
		spawn(function()
			DisappearEffect(npc.HumanoidRootPart, npc.HumanoidRootPart.Cloud)
		end)
		wait()
		npc:Destroy()
		wait(3)
		SpawnNpc(stage)

PlayParticleGroup Function

local function PlayParticalGroup(particlePart, emitAmnt, partPos, icon)
	local part = particlePart:Clone()
	part.Parent = workspace
	for i, partical in pairs(part:GetChildren()) do
		if partical.Name == "Icons" then
			partical.Texture = "rbxthumb://type=Asset&id=" .. tostring(icon) .. "&w=420&h=420"
		end
		part.Position = partPos
		partical:Emit(emitAmnt)
	end
end

SpawnNpc Function

local function SpawnNpc(stage)
	wait(0.65)
	--Npc
	local clone = npc:Clone()
	local hum = clone.Humanoid
	--Animations & Prompt
	local idleAnim = npc.Animation.Idle
	local prompt = clone.HumanoidRootPart.GiveJoke
	prompt.HoldDuration = player.Stages[stage.Configuration.StageName.Value].JokeTime.Value
	
	
	--Spawn
	clone.PrimaryPart.Position = GetRandomPos(stage.Position, stage.Size.X, stage.Size.Z, stage.Size.Y + 2.5)
	local npcAngleOffset = CFrame.Angles(0, math.rad(math.random(-360, 360)),0)
	clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * npcAngleOffset)
	
	clone.Parent = stage

	local idle = hum:LoadAnimation(idleAnim)
	--Gives Random Description
	idle:Play()

	local function iterPageItems(pages)
		return coroutine.wrap(function()
			local pagenum = 1

			while true do
				for _, item in ipairs(pages:GetCurrentPage()) do
					coroutine.yield(item, pagenum)
				end
				if pages.IsFinished then
					break
				end
				pages:AdvanceToNextPageAsync()
				pagenum = pagenum + 1
			end
		end)
	end
	local userId = Players:GetUserIdFromNameAsync(player.Name)

	local friendPages = Players:GetFriendsAsync(userId)

	local usernames = {}

	for item, _pageNo in iterPageItems(friendPages) do
		table.insert(usernames, item.Username)
	end

	local random = usernames[math.random(1, #usernames)]

	local randomuserid = Players:GetUserIdFromNameAsync(random)

	local desc = Players:GetHumanoidDescriptionFromUserId(randomuserid)

	hum:ApplyDescription(desc)
	
	clone.Name = random
	prompt.ObjectText = clone.Name
end

This issue has made it extremely difficult during testing and has over all been an annoyance.

All lag from the video is NOT from my computer, all that lag, including camera lag, is in studio and happens after the prompt is fired.

Can you please show a video with your lag spiking?

My apologies for not keeping up with the topic but the issue has been resolved, the script kept creating a new part every time a particle played therefore causing lag,

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.