Update CurrentRig and fix lag spike

I am making a system where npcs move in a line but theres an issue where when the event is fired once it moves the correct rig but when its fired again its supposed to move the next rig not the one that already moved correctly

Help me out :wink:

here is the decision logic

local function Descision(player : Player, currentRig : Model, chosen : boolean)
	if not player.Team then return end
	local airport = workspace.Airports:FindFirstChild(player.Team.Name)
	local passportData = Passengers.activePassengers[currentRig]
	if not currentRig.PrimaryPart then return end
	local prompt = currentRig.PrimaryPart:FindFirstChildWhichIsA("ProximityPrompt")
	
	local currentRig_Humanoid = currentRig:FindFirstChild("Humanoid")
	
	if chosen then
		if passportData.IsReal then
			-- Message/Color of message
			ReplicatedStorage.Events.Notification:FireClient(player, "Excellent!", Color3.fromRGB(56, 175, 56))
			RewardPlayer(player, 15)
		else
			ReplicatedStorage.Events.Notification:FireClient(player, "That was wrong. :(", Color3.fromRGB(175, 56, 56))
			currentRig:Destroy()
		end

		prompt.Enabled = false
		currentRig_Humanoid:MoveTo(airport.Accepted.Position)
		-- bagggage check
		Baggage:AddPassenger(currentRig)
	else

		if passportData.IsReal then
			ReplicatedStorage.Events.Notification:FireClient(player, "Incorrect try again!", Color3.fromRGB(175, 56, 56))
			currentRig:Destroy()
		else
			ReplicatedStorage.Events.Notification:FireClient(player, "Great Job!", Color3.fromRGB(56, 175, 56))
			RewardPlayer(player, 15)
		end

		prompt.Enabled = false
		currentRig_Humanoid:MoveTo(airport.Declined.Position)
		currentRig_Humanoid.MoveToFinished:Wait()
		Debris:AddItem(currentRig, 3)
	end
	currentRig = airport.PassengerWaypoints["4"].Rig
end

then the actual part of handling it

	ReplicatedStorage.Events.Passport.OnServerEvent:Connect(function(player, chosen)
		player.Character.Humanoid.WalkSpeed = 16
		player.Character.Humanoid.JumpHeight = 7.2
		local currentRig = airport.PassengerWaypoints["4"]:FindFirstChild("Rig")
		-- Handle the frontmost passenger (decision logic)
		Descision(player, currentRig, chosen)
		for i = 4, 2, -1 do
			local previousWaypoint = airport.PassengerWaypoints[tostring(i - 1)]
			local currentWaypoint = airport.PassengerWaypoints[tostring(i)]

			if previousWaypoint and previousWaypoint:FindFirstChild("Rig") then
				local rigToMove = previousWaypoint.Rig
				rigToMove.Parent = currentWaypoint
				rigToMove.Humanoid:MoveTo(currentWaypoint.Position)
			end
		end
	end)

theres probably an easy fix but can’t figure it out!

also the lag spikes are from when the rig is destroyed is there a better way of going about destroying them?

1 Like